Advertisement
andersonalmada

Untitled

Aug 26th, 2021
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require("express");
  2. const router = express.Router();
  3. const jsftp = require("jsftp");
  4. const fs = require("fs");
  5. require("dotenv/config");
  6.  
  7. const ftp = new jsftp({
  8.   host: process.env.FTP_HOST,
  9.   port: process.env.FTP_PORT,
  10.   user: process.env.FTP_USER,
  11.   pass: process.env.FTP_PASS,
  12. });
  13.  
  14. router.post("/", (req, res) => {
  15.   const formidable = require("formidable");
  16.   const form = new formidable.IncomingForm();
  17.   form.parse(req, (err, fields, files) => {
  18.     fs.readFile(files.file.path, function (err, buffer) {
  19.       if (err) {
  20.         console.error(err);
  21.       } else {
  22.         let obj = JSON.parse(fields.obj);
  23.         ftp.put(
  24.           buffer,
  25.           "public_html/uploads/" + obj.resource + "/" + obj.id,
  26.           (err) => {
  27.             if (!err) {
  28.               console.log("File transferred successfully!");
  29.             }
  30.             console.log(err);
  31.           }
  32.         );
  33.       }
  34.     });
  35.  
  36.     res.end();
  37.   });
  38. });
  39.  
  40. module.exports = router;
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement