Mark1928

MangaAPi

May 9th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const path = require("path");
  2. const fs = require("fs");
  3. const { google } = require("googleapis");
  4.  
  5. const drive = google.drive("v3");
  6. const key = require("./private_key.json");
  7.  
  8. const jwToken = new google.auth.JWT(
  9.   key.client_email,
  10.   null,
  11.   key.private_key,
  12.   ["https://www.googleapis.com/auth/drive"],
  13.   null
  14. );
  15.  
  16. jwToken.authorize((authErr) => {
  17.   if (authErr) {
  18.     console.log("authErr", authErr);
  19.     return;
  20.   }
  21.   console.log("Authorization Accorded");
  22. });
  23.  
  24. const parents = "1gm53UJwuodisMyVMmYRv1f2ISgCi5id3";
  25.  
  26. // Upload files
  27. exports.uploadFilesToGGDrive = (multerUpload) => {
  28.   const fileMetadata = {
  29.     name: multerUpload.originalname,
  30.     parents: [parents],
  31.   };
  32.   const media = {
  33.     mimeType: multerUpload.mimetype,
  34.     body: fs.createReadStream(multerUpload.path),
  35.   };
  36.   drive.files.create(
  37.     {
  38.       auth: jwToken,
  39.       resource: fileMetadata,
  40.       media,
  41.       fields: "id",
  42.     },
  43.     (err, file) => {
  44.       if (err) console.log("exports.uploadFilesToGGDrive -> err", err);
  45.       console.log(file);
  46.     }
  47.   );
  48. };
  49.  
  50. // Get all files name and ID in folder
  51. /* drive.files.list(
  52.   {
  53.     auth: jwToken,
  54.     pageSize: 10,
  55.     q: `'${parents}' in parents and trashed=false`,
  56.     fields: "files(id,name)",
  57.   },
  58.   (err, { data }) => {
  59.     if (err) return console.log("API returned an error ", err);
  60.     const { files } = data;
  61.     if (files.length) {
  62.       console.log("Files :", files);
  63.       files.map((file) => {
  64.         console.log(`${file.name} and ${file.id}`);
  65.       });
  66.     } else {
  67.       console.log("No files");
  68.     }
  69.   }
  70. ); */
  71.  
  72. /*
  73.   const media = {
  74.     mimeType:'image/jpg'
  75.     body: fs.createReadStream(path.join(__dirname,'))
  76.   }
  77. */
Add Comment
Please, Sign In to add comment