Просмотр исходного кода

add file for GridFS in file-uploader dir

yusueketk 7 лет назад
Родитель
Сommit
29229ecdbe
2 измененных файлов с 85 добавлено и 1 удалено
  1. 53 0
      src/server/service/file-uploader/gridfs.js
  2. 32 1
      yarn.lock

+ 53 - 0
src/server/service/file-uploader/gridfs.js

@@ -0,0 +1,53 @@
+// crowi-fileupload-gridFS
+
+module.exports = function(crowi) {
+  'use strict';
+
+  var debug = require('debug')('growi:service:fileUploaderLocal')
+  var fs = require('fs');
+  var mongoose = require('mongoose');
+  var lib = {}
+
+  // 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
+  var stream = Attachment.readById(objectid);
+
+  // for smaller file size
+  // read a file and receive a buffer
+  Attachment.readById(objectid, function (error, buffer) {
+    debug('Failed to read a file with ' + buffer, error);
+  });
+
+  // remove file details and its content from gridfs
+  Attachment.unlinkById(objectid, function (error, unlinkedAttachment) {
+    debug('Failed to remove ' + unlinkedAttachment + 'in gridFS', error);
+  });
+
+  return lib;
+};
+

+ 32 - 1
yarn.lock

@@ -31,6 +31,12 @@
   version "1.1.0"
   resolved "https://registry.yarnpkg.com/@handsontable/react/-/react-1.1.0.tgz#7f7cc822bc4cfab26f843792982ef81838e82d07"
 
+"@lykmapipo/gridfs-stream@^1.2.0":
+  version "1.2.0"
+  resolved "https://registry.yarnpkg.com/@lykmapipo/gridfs-stream/-/gridfs-stream-1.2.0.tgz#0f74826816b4f7414ae36862d67ce4849a224d91"
+  dependencies:
+    flushwritable "^1.0.0"
+
 "@sinonjs/formatio@^2.0.0":
   version "2.0.0"
   resolved "http://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz#84db7e9eb5531df18a8c5e0bfb6e449e55e654b2"
@@ -483,7 +489,7 @@ arrify@^1.0.0:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
 
-asap@~2.0.3:
+asap@^2.0.0, asap@~2.0.3:
   version "2.0.6"
   resolved "https://registry.yarnpkg.com/asap/-/asap-2.0.6.tgz#e50347611d7e690943208bbdafebcbc2fb866d46"
 
@@ -2628,6 +2634,13 @@ dev-ip@^1.0.1:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/dev-ip/-/dev-ip-1.0.1.tgz#a76a3ed1855be7a012bb8ac16cb80f3c00dc28f0"
 
+dezalgo@^1.0.1:
+  version "1.0.3"
+  resolved "https://registry.yarnpkg.com/dezalgo/-/dezalgo-1.0.3.tgz#7f742de066fc748bc8db820569dddce49bf0d456"
+  dependencies:
+    asap "^2.0.0"
+    wrappy "1"
+
 dicer@0.2.5:
   version "0.2.5"
   resolved "https://registry.yarnpkg.com/dicer/-/dicer-0.2.5.tgz#5996c086bb33218c812c090bddc09cd12facb70f"
@@ -3462,6 +3475,10 @@ flush-write-stream@^1.0.0:
     inherits "^2.0.1"
     readable-stream "^2.0.4"
 
+flushwritable@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/flushwritable/-/flushwritable-1.0.0.tgz#3e328d8fde412ad47e738e3be750b4d290043498"
+
 follow-redirects@^1.3.0:
   version "1.4.1"
   resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.4.1.tgz#d8120f4518190f55aac65bb6fc7b85fcd666d6aa"
@@ -5628,6 +5645,14 @@ mongodb@^2.0.36:
     mongodb-core "2.1.19"
     readable-stream "2.2.7"
 
+mongoose-gridfs@^0.5.0:
+  version "0.5.0"
+  resolved "https://registry.yarnpkg.com/mongoose-gridfs/-/mongoose-gridfs-0.5.0.tgz#626e12ab605c2ed2a205a5953cd5aa8615f44feb"
+  dependencies:
+    "@lykmapipo/gridfs-stream" "^1.2.0"
+    lodash "^4.17.10"
+    stream-read "^1.1.2"
+
 mongoose-legacy-pluralize@1.0.1:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.1.tgz#31ae25db45c30f1448c0f93f52769e903367c701"
@@ -8284,6 +8309,12 @@ stream-http@^2.7.2:
     to-arraybuffer "^1.0.0"
     xtend "^4.0.0"
 
+stream-read@^1.1.2:
+  version "1.1.2"
+  resolved "https://registry.yarnpkg.com/stream-read/-/stream-read-1.1.2.tgz#3137110d7aa80ba54e4b829c4cd33ca106b9564d"
+  dependencies:
+    dezalgo "^1.0.1"
+
 stream-shift@^1.0.0:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/stream-shift/-/stream-shift-1.0.0.tgz#d5c752825e5367e786f78e18e445ea223a155952"