| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- // crowi-fileupload-local
- module.exports = function(crowi) {
- 'use strict';
- var debug = require('debug')('crowi:lib:fileUploaderLocal')
- , fs = require('fs')
- , path = require('path')
- , mkdir = require('mkdirp')
- , Config = crowi.model('Config')
- , config = crowi.getConfig()
- , lib = {}
- , basePath = path.posix.join(crowi.publicDir, 'uploads'); // TODO: to configurable
- lib.deleteFile = function(fileId, filePath) {
- debug('File deletion: ' + filePath);
- return new Promise(function(resolve, reject) {
- fs.unlink(path.posix.join(basePath, filePath), function(err) {
- if (err) {
- debug(err);
- return reject(err);
- }
- resolve();
- });
- });
- };
- lib.uploadFile = function(filePath, contentType, fileStream, options) {
- debug('File uploading: ' + filePath);
- return new Promise(function(resolve, reject) {
- var localFilePath = path.posix.join(basePath, filePath)
- , dirpath = path.posix.dirname(localFilePath);
- mkdir(dirpath, function(err) {
- if (err) {
- return reject(err);
- }
- var writer = fs.createWriteStream(localFilePath);
- writer.on('error', function(err) {
- reject(err);
- }).on('finish', function() {
- resolve();
- });
- fileStream.pipe(writer);
- });
- });
- };
- lib.generateUrl = function(filePath) {
- return path.posix.join('/uploads', filePath);
- };
- lib.findDeliveryFile = function (fileId, filePath) {
- return Promise.resolve(lib.generateUrl(filePath));
- };
- return lib;
- };
|