-
Notifications
You must be signed in to change notification settings - Fork 9
/
ufs-gridfs.js
134 lines (123 loc) · 4.5 KB
/
ufs-gridfs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Karl STEIN
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
import { UploadFS } from 'meteor/jalik:ufs';
import { Meteor } from 'meteor/meteor';
/**
* GridFS store
* @param options
* @constructor
*/
export class GridFSStore extends UploadFS.Store {
constructor(options) {
// Default options
options = Object.assign({
chunkSize: 1024 * 255,
collectionName: 'uploadfs',
}, options);
// Check options
if (typeof options.chunkSize !== 'number') {
throw new TypeError('GridFSStore: chunkSize is not a number');
}
if (typeof options.collectionName !== 'string') {
throw new TypeError('GridFSStore: collectionName is not a string');
}
super(options);
this.chunkSize = parseInt(options.chunkSize);
this.collectionName = options.collectionName;
if (Meteor.isServer) {
let mongo = Package.mongo.MongoInternals.NpmModule;
let db = Package.mongo.MongoInternals.defaultRemoteCollectionDriver().mongo.db;
let mongoStore = new mongo.GridFSBucket(db, {
bucketName: options.collectionName,
chunkSizeBytes: this.chunkSize,
});
/**
* Removes the file
* @param fileId
* @param callback
*/
this.delete = function (fileId, callback) {
if (typeof callback !== 'function') {
callback = function (err) {
if (err) {
console.log('error');
}
};
}
const collectionName = options.collectionName + '.files';
db.collection(collectionName).findOne({ '_id': fileId }).then((file) => {
if (file) {
mongoStore.delete(fileId, callback);
}
});
};
/**
* Returns the file read stream
* @param fileId
* @param file
* @param options
* @return {*}
*/
this.getReadStream = function (fileId, file, options) {
options = Object.assign({}, options);
// https://mongodb.github.io/node-mongodb-native/4.4/interfaces/GridFSBucketReadStreamOptionsWithRevision.html#end
// according to the mongodb doc, the end is 0-based offset in bytes to stop streaming before -<< BEFORE
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range
// <range-end> an integer in the given unit indicating the end position (zero-indexed & inclusive) of the requested range. -<< inclusive
// so before there always one byte miss, then browser will send a start=end request, with case fail to get the DB's last byte
// this will leads to audio's duration Infinite and keep waiting...
if( options?.end ){
options.end += 1;
}
return mongoStore.openDownloadStream(fileId, options);
};
/**
* Returns the file write stream
* @param fileId
* @param file
* @param options
* @return {*}
*/
this.getWriteStream = function (fileId, file, options) {
let writeStream = mongoStore.openUploadStreamWithId(fileId, fileId, {
chunkSizeBytes: this.chunkSize,
contentType: file.type,
});
let finished = false;
writeStream.on('finish', function () {
finished = true;
});
writeStream.on('close', function () {
if (!finished) {
writeStream.emit('finish');
}
});
return writeStream;
};
}
}
}
// Add store to UFS namespace
UploadFS.store.GridFS = GridFSStore;