Meteor-files: Persistent storage with dropbox

Created on 5 Sep 2019  路  5Comments  路  Source: veliovgroup/Meteor-Files

I'm trying to store files on dropbox for my and have several questions. On production I used just this:

import { Meteor } from 'meteor/meteor';
import { FilesCollection } from 'meteor/ostrio:files';

const Images = new FilesCollection({
  collectionName: 'Images',
  allowClientCode: false, // Disallow remove files from Client
  onBeforeUpload(file) {
    // Allow upload files under 10MB, and only in png/jpg/jpeg formats
    if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
      return true;
    }
    return 'Please upload image, with size equal or less than 10MB';
  }
});

if (Meteor.isClient) {
  Meteor.subscribe('files.images.all');
}

if (Meteor.isServer) {
  Meteor.publish('files.images.all', function () {
    return Images.find().cursor;
  });
}

To integrate dropbox I tried slapping this from https://github.com/VeliovGroup/Meteor-Files/wiki/DropBox-Integration in

var Dropbox, Request, bound, client, fs, Collections = {};

if (Meteor.isServer) {
  Dropbox = Npm.require('dropbox');
  Request = Npm.require('request');
  fs = Npm.require('fs');
  bound = Meteor.bindEnvironment(function(callback) {
    return callback();
  });
  client = new Dropbox.Client({
    key: 'xxx',
    secret: 'xxx',
    token: 'xxxxxxxxxxxxxxxxxx'
  });
}

Collections.files = new FilesCollection({
  debug: false, // Change to `true` for debugging
  storagePath: 'assets/app/uploads/uploadedFiles',
  collectionName: 'uploadedFiles',
  allowClientCode: false,
  onAfterUpload: function(fileRef) {
    // In onAfterUpload callback we will move file to DropBox
    var self = this;
    var makeUrl = function(stat, fileRef, version, triesUrl) {
      if (triesUrl == null) {
        triesUrl = 0;
      }
      client.makeUrl(stat.path, {
        long: true,
        downloadHack: true
      }, function(error, xml) {
        // Store downloadable link in file's meta object
        bound(function() {
          if (error) {
            if (triesUrl < 10) {
              Meteor.setTimeout(function() {
                makeUrl(stat, fileRef, version, ++triesUrl);
              }, 2048);
            } else {
              console.error(error, {
                triesUrl: triesUrl
              });
            }
          } else if (xml) {
            var upd = {
              $set: {}
            };
            upd['$set']["versions." + version + ".meta.pipeFrom"] = xml.url;
            upd['$set']["versions." + version + ".meta.pipePath"] = stat.path;
            self.collection.update({
              _id: fileRef._id
            }, upd, function(error) {
              if (error) {
                console.error(error);
              } else {
                // Unlink original files from FS
                // after successful upload to DropBox
                self.unlink(self.collection.findOne(fileRef._id), version);
              }
            });
          } else {
            if (triesUrl < 10) {
              Meteor.setTimeout(function() {
                makeUrl(stat, fileRef, version, ++triesUrl);
              }, 2048);
            } else {
              console.error("client.makeUrl doesn't returns xml", {
                triesUrl: triesUrl
              });
            }
          }
        });
      });
    };

    var writeToDB = function(fileRef, version, data, triesSend) {
      // DropBox already uses random URLs
      // No need to use random file names
      if (triesSend == null) {
        triesSend = 0;
      }
      client.writeFile(fileRef._id + "-" + version + "." + fileRef.extension, data, function(error, stat) {
        bound(function() {
          if (error) {
            if (triesSend < 10) {
              Meteor.setTimeout(function() {
                writeToDB(fileRef, version, data, ++triesSend);
              }, 2048);
            } else {
              console.error(error, {
                triesSend: triesSend
              });
            }
          } else {
            // Generate downloadable link
            makeUrl(stat, fileRef, version);
          }
        });
      });
    };

    var readFile = function(fileRef, vRef, version, triesRead) {
      if (triesRead == null) {
        triesRead = 0;
      }
      fs.readFile(vRef.path, function(error, data) {
        bound(function() {
          if (error) {
            if (triesRead < 10) {
              readFile(fileRef, vRef, version, ++triesRead);
            } else {
              console.error(error);
            }
          } else {
            writeToDB(fileRef, version, data);
          }
        });
      });
    };

    var sendToStorage = function(fileRef) {
      _.each(fileRef.versions, function(vRef, version) {
        readFile(fileRef, vRef, version);
      });
    };

    sendToStorage(fileRef);
  },
  interceptDownload: function(http, fileRef, version) {
    var path, ref, ref1, ref2;
    path = (ref = fileRef.versions) != null ? (ref1 = ref[version]) != null ? (ref2 = ref1.meta) != null ? ref2.pipeFrom : void 0 : void 0 : void 0;
    if (path) {
      // If file is moved to DropBox
      // We will pipe request to DropBox
      // So, original link will stay always secure
      Request({
        url: path,
        headers: _.pick(http.request.headers, 'range', 'accept-language', 'accept', 'cache-control', 'pragma', 'connection', 'upgrade-insecure-requests', 'user-agent')
      }).pipe(http.response);
      return true;
    } else {
      // While file is not yet uploaded to DropBox
      // We will serve file from FS
      return false;
    }
  }
});

if (Meteor.isServer) {
  // Intercept File's collection remove method
  // to remove file from DropBox
  var _origRemove = Collections.files.remove;

  Collections.files.remove = function(search) {
    var cursor = this.collection.find(search);
    cursor.forEach(function(fileRef) {
      _.each(fileRef.versions, function(vRef) {
        var ref;
        if (vRef != null ? (ref = vRef.meta) != null ? ref.pipePath : void 0 : void 0) {
          client.remove(vRef.meta.pipePath, function(error) {
            bound(function() {
              if (error) {
                console.error(error);
              }
            });
          });
        }
      });
    });
    // Call original method
    _origRemove.call(this, search);
  };
}

I get this error:

W20190905-13:42:46.552(2)? (STDERR) C:\Users\NERV\AppData\Local\.meteor\packages\meteor-tool\1.8.1\mt-os.windows.x86_64\dev_bundle\server-lib\node_modules\fibers\future.js:280
W20190905-13:42:46.554(2)? (STDERR)                                             throw(ex);
W20190905-13:42:46.555(2)? (STDERR)                                             ^
W20190905-13:42:46.556(2)? (STDERR)
W20190905-13:42:46.557(2)? (STDERR) Error: Cannot find module "request"
W20190905-13:42:46.558(2)? (STDERR)     at Object.require (C:\Users\NERV\Desktop\Projekte\DDCFull\.meteor\local\build\programs\server\boot.js:303:24)
W20190905-13:42:46.558(2)? (STDERR)     at images.js (imports/api/images/images.js:11:17)
W20190905-13:42:46.559(2)? (STDERR)     at fileEvaluate (packages\modules-runtime.js:336:7)
W20190905-13:42:46.560(2)? (STDERR)     at Module.require (packages\modules-runtime.js:238:14)
W20190905-13:42:46.561(2)? (STDERR)     at Module.moduleLink [as link] (C:\Users\NERV\AppData\Local\.meteor\packages\modules\0.13.0\npm\node_modules\reify\lib\runtime\index.js:38:38)
W20190905-13:42:46.562(2)? (STDERR)     at methods.js (imports/api/characters/methods.js:1:463)
W20190905-13:42:46.565(2)? (STDERR)     at fileEvaluate (packages\modules-runtime.js:336:7)
W20190905-13:42:46.566(2)? (STDERR)     at Module.require (packages\modules-runtime.js:238:14)
W20190905-13:42:46.567(2)? (STDERR)     at Module.moduleLink [as link] (C:\Users\NERV\AppData\Local\.meteor\packages\modules\0.13.0\npm\node_modules\reify\lib\runtime\index.js:38:38)
W20190905-13:42:46.568(2)? (STDERR)     at main.js (server/main.js:1:146)
W20190905-13:42:46.568(2)? (STDERR)     at fileEvaluate (packages\modules-runtime.js:336:7)
W20190905-13:42:46.569(2)? (STDERR)     at Module.require (packages\modules-runtime.js:238:14)
W20190905-13:42:46.570(2)? (STDERR)     at require (packages\modules-runtime.js:258:21)
W20190905-13:42:46.571(2)? (STDERR)     at C:\Users\NERV\Desktop\Projekte\DDCFull\.meteor\local\build\programs\server\app\app.js:4673:1
W20190905-13:42:46.571(2)? (STDERR)     at C:\Users\NERV\Desktop\Projekte\DDCFull\.meteor\local\build\programs\server\boot.js:419:36
W20190905-13:42:46.573(2)? (STDERR)     at Array.forEach (<anonymous>)

So since it cannot find the module "request" I tried downloading this package: https://www.npmjs.com/package/require But I'm not sure if thats what it needs and if it does how to import it. I can't seem to find it in my node-modules directory.

How can I import this needed module into the file? Thank you for any help!

DropBox question

All 5 comments

Hello @TomokoOG ,

Very sorry to tell you that, but our DropBox integration guide is outdated and not supported by DropBox API, see this issue

You can solve your issue by simply installing request library with:

meteor npm install request --save

And changing

  Dropbox = Npm.require('dropbox');
  Request = Npm.require('request');

to

  Dropbox = require('dropbox');
  Request = require('request');

Okay, thanks for the reply. I guess I try something else then.

Is the google cloud storage guide still up to date?
https://github.com/VeliovGroup/Meteor-Files/wiki/Google-Cloud-Storage-Integration

@tomokoog yes it鈥檚 up to date, to check documentation status and its history you an check or docs directory (from where it goes to wiki)

Oh I didn't know. Still new to Github. Thank you!

@TomokoOG no worries, it's always better to ask, than waste your time 馃槈
Hope I helped you at least a little

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JanSchuermannPH picture JanSchuermannPH  路  3Comments

ck23onGithub picture ck23onGithub  路  3Comments

stefanve picture stefanve  路  4Comments

sabedinia picture sabedinia  路  3Comments

rlhk picture rlhk  路  4Comments