2
0

fileStoreStrategy.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import fs from 'fs';
  2. import { createObjectId } from './grid/createObjectId';
  3. import { createOnAfterUpload } from './fsHooks/createOnAfterUpload';
  4. import { createInterceptDownload } from './fsHooks/createInterceptDownload';
  5. import { createOnAfterRemove } from './fsHooks/createOnAfterRemove';
  6. /** Factory for FileStoreStrategy */
  7. export default class FileStoreStrategyFactory {
  8. /** constructor
  9. * @param classFileStoreStrategyFilesystem use this strategy for filesystem storage
  10. * @param classFileStoreStrategyGridFs use this strategy for GridFS storage
  11. * @param gridFsBucket use this GridFS Bucket as GridFS Storage
  12. */
  13. constructor(classFileStoreStrategyFilesystem, classFileStoreStrategyGridFs, gridFsBucket) {
  14. this.classFileStoreStrategyFilesystem = classFileStoreStrategyFilesystem;
  15. this.classFileStoreStrategyGridFs = classFileStoreStrategyGridFs;
  16. this.gridFsBucket = gridFsBucket;
  17. }
  18. /** returns the right FileStoreStrategy
  19. * @param filesCollection the current FilesCollection instance
  20. * @param fileObj the current file object
  21. * @param versionName the current version
  22. * @param use this storage, or if not set, get the storage from fileObj
  23. */
  24. getFileStrategy(filesCollection, fileObj, versionName, storage) {
  25. if (!storage) {
  26. storage = fileObj.versions[versionName].storage || "gridfs";
  27. }
  28. let ret;
  29. if (["fs", "gridfs"].includes(storage)) {
  30. if (storage == "fs") {
  31. ret = new this.classFileStoreStrategyFilesystem(filesCollection, fileObj, versionName);
  32. } else if (storage == "gridfs") {
  33. ret = new this.classFileStoreStrategyGridFs(this.gridFsBucket, filesCollection, fileObj, versionName);
  34. }
  35. }
  36. return ret;
  37. }
  38. }
  39. /** Strategy to store files */
  40. class FileStoreStrategy {
  41. /** constructor
  42. * @param filesCollection the current FilesCollection instance
  43. * @param fileObj the current file object
  44. * @param versionName the current version
  45. */
  46. constructor(filesCollection, fileObj, versionName) {
  47. this.filesCollection = filesCollection;
  48. this.fileObj = fileObj;
  49. this.versionName = versionName;
  50. }
  51. /** after successfull upload */
  52. onAfterUpload() {
  53. }
  54. /** download the file
  55. * @param http the current http request
  56. */
  57. interceptDownload(http) {
  58. }
  59. /** after file remove */
  60. onAfterRemove() {
  61. }
  62. /** returns a read stream
  63. * @return the read stream
  64. */
  65. getReadStream() {
  66. }
  67. /** returns a write stream
  68. * @return the write stream
  69. */
  70. getWriteStream() {
  71. }
  72. /** writing finished
  73. * @param finishedData the data of the write stream finish event
  74. */
  75. writeStreamFinished(finishedData) {
  76. }
  77. /** remove the file */
  78. unlink() {
  79. }
  80. /** return the storage name
  81. * @return the storage name
  82. */
  83. getStorageName() {
  84. }
  85. }
  86. /** Strategy to store attachments at GridFS (MongoDB) */
  87. export class FileStoreStrategyGridFs extends FileStoreStrategy {
  88. /** constructor
  89. * @param gridFsBucket use this GridFS Bucket
  90. * @param filesCollection the current FilesCollection instance
  91. * @param fileObj the current file object
  92. * @param versionName the current version
  93. */
  94. constructor(gridFsBucket, filesCollection, fileObj, versionName) {
  95. super(filesCollection, fileObj, versionName);
  96. this.gridFsBucket = gridFsBucket;
  97. }
  98. /** after successfull upload */
  99. onAfterUpload() {
  100. createOnAfterUpload(this.filesCollection, this.gridFsBucket, this.fileObj, this.versionName);
  101. super.onAfterUpload();
  102. }
  103. /** download the file
  104. * @param http the current http request
  105. */
  106. interceptDownload(http) {
  107. const ret = createInterceptDownload(this.filesCollection, this.gridFsBucket, this.fileObj, http, this.versionName);
  108. return ret;
  109. }
  110. /** after file remove */
  111. onAfterRemove() {
  112. this.unlink();
  113. super.onAfterRemove();
  114. }
  115. /** returns a read stream
  116. * @return the read stream
  117. */
  118. getReadStream() {
  119. const gridFsFileId = (this.fileObj.versions[this.versionName].meta || {})
  120. .gridFsFileId;
  121. let ret;
  122. if (gridFsFileId) {
  123. const gfsId = createObjectId({ gridFsFileId });
  124. ret = this.gridFsBucket.openDownloadStream(gfsId);
  125. }
  126. return ret;
  127. }
  128. /** returns a write stream
  129. * @return the write stream
  130. */
  131. getWriteStream() {
  132. const fileObj = this.fileObj;
  133. const versionName = this.versionName;
  134. const metadata = { ...fileObj.meta, versionName, fileId: fileObj._id };
  135. const ret = this.gridFsBucket.openUploadStream(this.fileObj.name, {
  136. contentType: fileObj.type || 'binary/octet-stream',
  137. metadata,
  138. });
  139. return ret;
  140. }
  141. /** writing finished
  142. * @param finishedData the data of the write stream finish event
  143. */
  144. writeStreamFinished(finishedData) {
  145. const gridFsFileIdName = this.getGridFsFileIdName();
  146. Attachments.update({ _id: this.fileObj._id }, { $set: { [gridFsFileIdName]: finishedData._id.toHexString(), } });
  147. }
  148. /** remove the file */
  149. unlink() {
  150. createOnAfterRemove(this.filesCollection, this.gridFsBucket, this.fileObj, this.versionName);
  151. const gridFsFileIdName = this.getGridFsFileIdName();
  152. Attachments.update({ _id: this.fileObj._id }, { $unset: { [gridFsFileIdName]: 1 } });
  153. }
  154. /** return the storage name
  155. * @return the storage name
  156. */
  157. getStorageName() {
  158. return "gridfs";
  159. }
  160. /** returns the property name of gridFsFileId
  161. * @return the property name of gridFsFileId
  162. */
  163. getGridFsFileIdName() {
  164. const ret = `versions.${this.versionName}.meta.gridFsFileId`;
  165. return ret;
  166. }
  167. }
  168. /** Strategy to store attachments at filesystem */
  169. export class FileStoreStrategyFilesystem extends FileStoreStrategy {
  170. /** constructor
  171. * @param filesCollection the current FilesCollection instance
  172. * @param fileObj the current file object
  173. * @param versionName the current version
  174. */
  175. constructor(filesCollection, fileObj, versionName) {
  176. super(filesCollection, fileObj, versionName);
  177. }
  178. /** returns a read stream
  179. * @return the read stream
  180. */
  181. getReadStream() {
  182. const ret = fs.createReadStream(this.fileObj.versions[this.versionName].path)
  183. return ret;
  184. }
  185. /** returns a write stream
  186. * @return the write stream
  187. */
  188. getWriteStream() {
  189. const filePath = this.fileObj.versions[this.versionName].path;
  190. const ret = fs.createWriteStream(filePath);
  191. return ret;
  192. }
  193. /** writing finished
  194. * @param finishedData the data of the write stream finish event
  195. */
  196. writeStreamFinished(finishedData) {
  197. }
  198. /** remove the file */
  199. unlink() {
  200. const filePath = this.fileObj.versions[this.versionName].path;
  201. fs.unlink(filePath, () => {});
  202. }
  203. /** return the storage name
  204. * @return the storage name
  205. */
  206. getStorageName() {
  207. return "fs";
  208. }
  209. }
  210. /** move the fileObj to another storage
  211. * @param fileObj move this fileObj to another storage
  212. * @param storageDestination the storage destination (fs or gridfs)
  213. * @param fileStoreStrategyFactory get FileStoreStrategy from this factory
  214. */
  215. export const moveToStorage = function(fileObj, storageDestination, fileStoreStrategyFactory) {
  216. Object.keys(fileObj.versions).forEach(versionName => {
  217. const strategyRead = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName);
  218. const strategyWrite = fileStoreStrategyFactory.getFileStrategy(this, fileObj, versionName, storageDestination);
  219. if (strategyRead.constructor.name != strategyWrite.constructor.name) {
  220. const readStream = strategyRead.getReadStream();
  221. const writeStream = strategyWrite.getWriteStream();
  222. writeStream.on('error', error => {
  223. console.error('[writeStream error]: ', error, fileObjId);
  224. });
  225. readStream.on('error', error => {
  226. console.error('[readStream error]: ', error, fileObjId);
  227. });
  228. writeStream.on('finish', Meteor.bindEnvironment((finishedData) => {
  229. strategyWrite.writeStreamFinished(finishedData);
  230. }));
  231. // https://forums.meteor.com/t/meteor-code-must-always-run-within-a-fiber-try-wrapping-callbacks-that-you-pass-to-non-meteor-libraries-with-meteor-bindenvironmen/40099/8
  232. readStream.on('end', Meteor.bindEnvironment(() => {
  233. Attachments.update({ _id: fileObj._id }, { $set: { [`versions.${versionName}.storage`]: strategyWrite.getStorageName() } });
  234. strategyRead.unlink();
  235. }));
  236. readStream.pipe(writeStream);
  237. }
  238. });
  239. };