|
|
@@ -8,57 +8,33 @@ module.exports = function(crowi) {
|
|
|
var mongoose = require('mongoose');
|
|
|
var gridfs = require('gridfs-stream');
|
|
|
var path = require('path');
|
|
|
- var basePath = path.posix.join(crowi.publicDir, 'uploads'); // TODO: to configurable
|
|
|
-
|
|
|
var lib = {};
|
|
|
+ var AttachmentFile = {};
|
|
|
|
|
|
- mongoose.connect('mongodb://localhost/growi', {
|
|
|
- useNewUrlParser: true
|
|
|
+ // instantiate mongoose-gridfs
|
|
|
+ var gridfs = require('mongoose-gridfs')({
|
|
|
+ collection: 'attachments',
|
|
|
+ model: 'AttachmentFile',
|
|
|
+ mongooseConnection: mongoose.connection
|
|
|
});
|
|
|
- mongoose.Promise = global.Promise;
|
|
|
- gridfs.mongo = mongoose.mongo;
|
|
|
- var connection = mongoose.connection;
|
|
|
- connection.on('error', console.error.bind(console, 'connection error:'));
|
|
|
|
|
|
+ // obtain a model
|
|
|
+ AttachmentFile= gridfs.model;
|
|
|
+
|
|
|
+ // create or save a file
|
|
|
lib.uploadFile = function (filePath, contentType, fileStream, options) {
|
|
|
- debug('File uploading: ' + filePath);
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
- // connection.once('open', function () {
|
|
|
- var gfs = gridfs(connection.db);
|
|
|
-
|
|
|
- // Writing a file from local to MongoDB
|
|
|
- var writestream = gfs.createWriteStream({ filename: 'test.jpg' });
|
|
|
- fs.createReadStream(filePath).pipe(writestream);
|
|
|
- writestream.on('close', function (file) {
|
|
|
- resolve(file);
|
|
|
+ AttachmentFile.write({
|
|
|
+ filename: 'test1.jpg',
|
|
|
+ contentType: contentType
|
|
|
+ },
|
|
|
+ fs.createReadStream(fileStream.path),
|
|
|
+ function (error, createdFile) {
|
|
|
+ debug('Failed to upload ' + createdFile + 'to gridFS', error);
|
|
|
+ resolve(createdFile);
|
|
|
});
|
|
|
});
|
|
|
- // });
|
|
|
};
|
|
|
- // // mongoose connect
|
|
|
- // mongoose.connect('mongodb://localhost/test');
|
|
|
-
|
|
|
- // // instantiate mongoose-gridfs
|
|
|
- // var gridfs = require('mongoose-gridfs')({
|
|
|
- // collection: 'attachments',
|
|
|
- // model: 'Attachment',
|
|
|
- // mongooseConnection: mongoose.connection
|
|
|
- // });
|
|
|
-
|
|
|
- // // obtain a model
|
|
|
- // Attachment = gridfs.model;
|
|
|
-
|
|
|
- // // create or save a file
|
|
|
- // lib.uploadFile = function (filePath, contentType, fileStream, options) {
|
|
|
- // Attachment.write({
|
|
|
- // filePath: filePath,
|
|
|
- // contentType: contentType
|
|
|
- // },
|
|
|
- // fs.createReadStream(fileStream.path),
|
|
|
- // function (error, createdFile) {
|
|
|
- // debug('Failed to upload ' + createdFile + 'to gridFS', error);
|
|
|
- // });
|
|
|
- // };
|
|
|
|
|
|
// for larger file size
|
|
|
// read a file and receive a stream
|
|
|
@@ -74,6 +50,7 @@ module.exports = function(crowi) {
|
|
|
// Attachment.unlinkById(objectid, function (error, unlinkedAttachment) {
|
|
|
// debug('Failed to remove ' + unlinkedAttachment + 'in gridFS', error);
|
|
|
// });
|
|
|
+
|
|
|
lib.generateUrl = function (filePath) {
|
|
|
return path.posix.join('/uploads', filePath);
|
|
|
};
|