|
|
@@ -4,9 +4,7 @@ module.exports = function(crowi) {
|
|
|
'use strict';
|
|
|
|
|
|
var debug = require('debug')('growi:service:fileUploaderLocal')
|
|
|
- var fs = require('fs');
|
|
|
var mongoose = require('mongoose');
|
|
|
- var gridfs = require('gridfs-stream');
|
|
|
var path = require('path');
|
|
|
var lib = {};
|
|
|
var AttachmentFile = {};
|
|
|
@@ -19,34 +17,31 @@ module.exports = function(crowi) {
|
|
|
});
|
|
|
|
|
|
// obtain a model
|
|
|
- AttachmentFile= gridfs.model;
|
|
|
+ AttachmentFile = gridfs.model;
|
|
|
|
|
|
// delete a file
|
|
|
- lib.deleteFile = function (fileId, filePath) {
|
|
|
+ lib.deleteFile = async function(fileId, filePath) {
|
|
|
debug('File deletion: ' + fileId);
|
|
|
- return new Promise(function (resolve, reject) {
|
|
|
- AttachmentFile.unlinkById(fileId, function (error, unlinkedAttachment) {
|
|
|
- resolve();
|
|
|
- });
|
|
|
+ await AttachmentFile.unlinkById(fileId, function(error, unlinkedAttachment) {
|
|
|
+ if (error) {
|
|
|
+ throw new Error(error);
|
|
|
+ }
|
|
|
});
|
|
|
};
|
|
|
|
|
|
// create or save a file
|
|
|
- lib.uploadFile = function (filePath, contentType, fileStream, options) {
|
|
|
- return new Promise(function (resolve, reject) {
|
|
|
- AttachmentFile.write({
|
|
|
- filename: filePath,
|
|
|
- contentType: contentType
|
|
|
- },
|
|
|
- fileStream,
|
|
|
- function (error, createdFile) {
|
|
|
- debug('Failed to upload ' + createdFile + 'to gridFS', error);
|
|
|
- resolve(createdFile._id);
|
|
|
+ lib.uploadFile = async function(filePath, contentType, fileStream, options) {
|
|
|
+ debug('File uploading: ' + filePath);
|
|
|
+ await AttachmentFile.write({filename: filePath, contentType: contentType}, fileStream,
|
|
|
+ function(error, createdFile) {
|
|
|
+ if (error) {
|
|
|
+ throw new Error('Failed to upload ' + createdFile + 'to gridFS', error);
|
|
|
+ }
|
|
|
+ return createdFile._id;
|
|
|
});
|
|
|
- });
|
|
|
};
|
|
|
|
|
|
- lib.generateUrl = function (filePath) {
|
|
|
+ lib.generateUrl = function(filePath) {
|
|
|
return path.posix.join('/uploads', filePath);
|
|
|
};
|
|
|
|