Advertisement
adamatti

Deta - Upload/Download

Sep 6th, 2022 (edited)
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Deta } = require("deta");
  2. const express = require("express");
  3. const upload = require("express-fileupload");
  4.  
  5. const app = express();
  6.  
  7. app.use(upload());
  8.  
  9. const deta = Deta("REMOVED");
  10. const drive = deta.Drive("images");
  11.  
  12. app.get('/', (req, res) => {
  13.     res.send(`
  14.     <form action="/upload" enctype="multipart/form-data" method="post">
  15.       <input type="file" name="file">
  16.       <input type="submit" value="Upload">
  17.     </form>`);
  18. });
  19.  
  20. app.post("/upload", async (req, res) => {
  21.     const name = req.files.file.name;
  22.     const contents = req.files.file.data;
  23.     const img = await drive.put(name, {data: contents});
  24.     res.send(img);
  25. });
  26.  
  27. app.get("/download/:name", async (req, res) => {
  28.     const name = req.params.name;
  29.     const img = await drive.get(name);
  30.     const buffer = await img.arrayBuffer();
  31.     res.send(Buffer.from(buffer));
  32. });
  33.  
  34. module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement