Skip to content

Commit 370215a

Browse files
authored
Support Metadata in GridFSAdapter (#6660)
* Support Metadata in GridFSAdapter * Useful for testing in the JS SDK * Adds new endpoint to be used with `Parse.File.getData` * Allows file adapters to return tags as well as future data. * fix tests * Make getMetadata optional * Revert "fix tests" This reverts commit 7706da1. * improve coverage
1 parent c32ff20 commit 370215a

File tree

6 files changed

+162
-50
lines changed

6 files changed

+162
-50
lines changed

spec/FilesController.spec.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const GridStoreAdapter = require('../lib/Adapters/Files/GridStoreAdapter')
88
.GridStoreAdapter;
99
const Config = require('../lib/Config');
1010
const FilesController = require('../lib/Controllers/FilesController').default;
11+
const databaseURI = 'mongodb://localhost:27017/parse';
1112

1213
const mockAdapter = {
1314
createFile: () => {
@@ -23,13 +24,13 @@ const mockAdapter = {
2324

2425
// Small additional tests to improve overall coverage
2526
describe('FilesController', () => {
26-
it('should properly expand objects', done => {
27+
it('should properly expand objects', (done) => {
2728
const config = Config.get(Parse.applicationId);
2829
const gridStoreAdapter = new GridFSBucketAdapter(
2930
'mongodb://localhost:27017/parse'
3031
);
3132
const filesController = new FilesController(gridStoreAdapter);
32-
const result = filesController.expandFilesInObject(config, function() {});
33+
const result = filesController.expandFilesInObject(config, function () {});
3334

3435
expect(result).toBeUndefined();
3536

@@ -47,7 +48,7 @@ describe('FilesController', () => {
4748
done();
4849
});
4950

50-
it('should create a server log on failure', done => {
51+
it('should create a server log on failure', (done) => {
5152
const logController = new LoggerController(new WinstonLoggerAdapter());
5253

5354
reconfigureServer({ filesAdapter: mockAdapter })
@@ -56,30 +57,28 @@ describe('FilesController', () => {
5657
() => done.fail('should not succeed'),
5758
() => setImmediate(() => Promise.resolve('done'))
5859
)
59-
.then(() => new Promise(resolve => setTimeout(resolve, 200)))
60+
.then(() => new Promise((resolve) => setTimeout(resolve, 200)))
6061
.then(() =>
6162
logController.getLogs({ from: Date.now() - 1000, size: 1000 })
6263
)
63-
.then(logs => {
64+
.then((logs) => {
6465
// we get two logs here: 1. the source of the failure to save the file
6566
// and 2 the message that will be sent back to the client.
6667

6768
const log1 = logs.find(
68-
x => x.message === 'Error creating a file: it failed with xyz'
69+
(x) => x.message === 'Error creating a file: it failed with xyz'
6970
);
7071
expect(log1.level).toBe('error');
7172

72-
const log2 = logs.find(
73-
x => x.message === 'it failed with xyz'
74-
);
73+
const log2 = logs.find((x) => x.message === 'it failed with xyz');
7574
expect(log2.level).toBe('error');
7675
expect(log2.code).toBe(130);
7776

7877
done();
7978
});
8079
});
8180

82-
it('should create a parse error when a string is returned', done => {
81+
it('should create a parse error when a string is returned', (done) => {
8382
const mock2 = mockAdapter;
8483
mock2.validateFilename = () => {
8584
return 'Bad file! No biscuit!';
@@ -92,7 +91,7 @@ describe('FilesController', () => {
9291
done();
9392
});
9493

95-
it('should add a unique hash to the file name when the preserveFileName option is false', done => {
94+
it('should add a unique hash to the file name when the preserveFileName option is false', (done) => {
9695
const config = Config.get(Parse.applicationId);
9796
const gridStoreAdapter = new GridFSBucketAdapter(
9897
'mongodb://localhost:27017/parse'
@@ -115,7 +114,7 @@ describe('FilesController', () => {
115114
done();
116115
});
117116

118-
it('should not add a unique hash to the file name when the preserveFileName option is true', done => {
117+
it('should not add a unique hash to the file name when the preserveFileName option is true', (done) => {
119118
const config = Config.get(Parse.applicationId);
120119
const gridStoreAdapter = new GridFSBucketAdapter(
121120
'mongodb://localhost:27017/parse'
@@ -137,7 +136,16 @@ describe('FilesController', () => {
137136
done();
138137
});
139138

140-
it('should reject slashes in file names', done => {
139+
it('should handle adapter without getMetadata', async () => {
140+
const gridStoreAdapter = new GridFSBucketAdapter(databaseURI);
141+
gridStoreAdapter.getMetadata = null;
142+
const filesController = new FilesController(gridStoreAdapter);
143+
144+
const result = await filesController.getMetadata();
145+
expect(result).toEqual({});
146+
});
147+
148+
it('should reject slashes in file names', (done) => {
141149
const gridStoreAdapter = new GridFSBucketAdapter(
142150
'mongodb://localhost:27017/parse'
143151
);
@@ -146,7 +154,7 @@ describe('FilesController', () => {
146154
done();
147155
});
148156

149-
it('should also reject slashes in file names', done => {
157+
it('should also reject slashes in file names', (done) => {
150158
const gridStoreAdapter = new GridStoreAdapter(
151159
'mongodb://localhost:27017/parse'
152160
);

spec/GridFSBucketStorageAdapter.spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const GridFSBucketAdapter = require('../lib/Adapters/Files/GridFSBucketAdapter')
44
.GridFSBucketAdapter;
55
const { randomString } = require('../lib/cryptoUtils');
66
const databaseURI = 'mongodb://localhost:27017/parse';
7+
const request = require('../lib/request');
8+
const Config = require('../lib/Config');
79

810
async function expectMissingFile(gfsAdapter, name) {
911
try {
@@ -33,6 +35,73 @@ describe_only_db('mongo')('GridFSBucket and GridStore interop', () => {
3335
expect(gfsResult.toString('utf8')).toBe(originalString);
3436
});
3537

38+
it('should save metadata', async () => {
39+
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
40+
const originalString = 'abcdefghi';
41+
const metadata = { hello: 'world' };
42+
await gfsAdapter.createFile('myFileName', originalString, null, {
43+
metadata,
44+
});
45+
const gfsResult = await gfsAdapter.getFileData('myFileName');
46+
expect(gfsResult.toString('utf8')).toBe(originalString);
47+
let gfsMetadata = await gfsAdapter.getMetadata('myFileName');
48+
expect(gfsMetadata.metadata).toEqual(metadata);
49+
50+
// Empty json for file not found
51+
gfsMetadata = await gfsAdapter.getMetadata('myUnknownFile');
52+
expect(gfsMetadata).toEqual({});
53+
});
54+
55+
it('should save metadata with file', async () => {
56+
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
57+
await reconfigureServer({ filesAdapter: gfsAdapter });
58+
const str = 'Hello World!';
59+
const data = [];
60+
for (let i = 0; i < str.length; i++) {
61+
data.push(str.charCodeAt(i));
62+
}
63+
const metadata = { foo: 'bar' };
64+
const file = new Parse.File('hello.txt', data, 'text/plain');
65+
file.addMetadata('foo', 'bar');
66+
await file.save();
67+
let fileData = await gfsAdapter.getMetadata(file.name());
68+
expect(fileData.metadata).toEqual(metadata);
69+
70+
// Can only add metadata on create
71+
file.addMetadata('hello', 'world');
72+
await file.save();
73+
fileData = await gfsAdapter.getMetadata(file.name());
74+
expect(fileData.metadata).toEqual(metadata);
75+
76+
const headers = {
77+
'X-Parse-Application-Id': 'test',
78+
'X-Parse-REST-API-Key': 'rest',
79+
};
80+
const response = await request({
81+
method: 'GET',
82+
headers,
83+
url: `http://localhost:8378/1/files/test/metadata/${file.name()}`,
84+
});
85+
fileData = response.data;
86+
expect(fileData.metadata).toEqual(metadata);
87+
});
88+
89+
it('should handle getMetadata error', async () => {
90+
const config = Config.get('test');
91+
config.filesController.getMetadata = () => Promise.reject();
92+
93+
const headers = {
94+
'X-Parse-Application-Id': 'test',
95+
'X-Parse-REST-API-Key': 'rest',
96+
};
97+
const response = await request({
98+
method: 'GET',
99+
headers,
100+
url: `http://localhost:8378/1/files/test/metadata/filename.txt`,
101+
});
102+
expect(response.data).toEqual({});
103+
});
104+
36105
it('properly fetches a large file from GridFS', async () => {
37106
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
38107
const twoMegabytesFile = randomString(2048 * 1024);

src/Adapters/Files/FilesAdapter.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ export class FilesAdapter {
8888
* @returns {Promise} Data for byte range
8989
*/
9090
// handleFileStream(filename: string, res: any, req: any, contentType: string): Promise
91+
92+
/** Responsible for retrieving metadata and tags
93+
*
94+
* @param {string} filename - the filename to retrieve metadata
95+
*
96+
* @return {Promise} a promise that should pass with metadata
97+
*/
98+
// getMetadata(filename: string): Promise<any> {}
9199
}
92100

93101
/**

src/Adapters/Files/GridFSBucketAdapter.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class GridFSBucketAdapter extends FilesAdapter {
3232
this._connectionPromise = MongoClient.connect(
3333
this._databaseURI,
3434
this._mongoOptions
35-
).then(client => {
35+
).then((client) => {
3636
this._client = client;
3737
return client.db(client.s.options.dbName);
3838
});
@@ -41,14 +41,16 @@ export class GridFSBucketAdapter extends FilesAdapter {
4141
}
4242

4343
_getBucket() {
44-
return this._connect().then(database => new GridFSBucket(database));
44+
return this._connect().then((database) => new GridFSBucket(database));
4545
}
4646

4747
// For a given config object, filename, and data, store a file
4848
// Returns a promise
49-
async createFile(filename: string, data) {
49+
async createFile(filename: string, data, contentType, options = {}) {
5050
const bucket = await this._getBucket();
51-
const stream = await bucket.openUploadStream(filename);
51+
const stream = await bucket.openUploadStream(filename, {
52+
metadata: options.metadata,
53+
});
5254
await stream.write(data);
5355
stream.end();
5456
return new Promise((resolve, reject) => {
@@ -64,7 +66,7 @@ export class GridFSBucketAdapter extends FilesAdapter {
6466
throw new Error('FileNotFound');
6567
}
6668
return Promise.all(
67-
documents.map(doc => {
69+
documents.map((doc) => {
6870
return bucket.delete(doc._id);
6971
})
7072
);
@@ -76,13 +78,13 @@ export class GridFSBucketAdapter extends FilesAdapter {
7678
stream.read();
7779
return new Promise((resolve, reject) => {
7880
const chunks = [];
79-
stream.on('data', data => {
81+
stream.on('data', (data) => {
8082
chunks.push(data);
8183
});
8284
stream.on('end', () => {
8385
resolve(Buffer.concat(chunks));
8486
});
85-
stream.on('error', err => {
87+
stream.on('error', (err) => {
8688
reject(err);
8789
});
8890
});
@@ -98,6 +100,16 @@ export class GridFSBucketAdapter extends FilesAdapter {
98100
);
99101
}
100102

103+
async getMetadata(filename) {
104+
const bucket = await this._getBucket();
105+
const files = await bucket.find({ filename }).toArray();
106+
if (files.length === 0) {
107+
return {};
108+
}
109+
const { metadata } = files[0];
110+
return { metadata };
111+
}
112+
101113
async handleFileStream(filename: string, req, res, contentType) {
102114
const bucket = await this._getBucket();
103115
const files = await bucket.find({ filename }).toArray();
@@ -122,7 +134,7 @@ export class GridFSBucketAdapter extends FilesAdapter {
122134
});
123135
const stream = bucket.openDownloadStreamByName(filename);
124136
stream.start(start);
125-
stream.on('data', chunk => {
137+
stream.on('data', (chunk) => {
126138
res.write(chunk);
127139
});
128140
stream.on('error', () => {

src/Controllers/FilesController.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,21 @@ export class FilesController extends AdaptableController {
4545
return this.adapter.deleteFile(filename);
4646
}
4747

48+
getMetadata(filename) {
49+
if (typeof this.adapter.getMetadata === 'function') {
50+
return this.adapter.getMetadata(filename);
51+
}
52+
return Promise.resolve({});
53+
}
54+
4855
/**
4956
* Find file references in REST-format object and adds the url key
5057
* with the current mount point and app id.
5158
* Object may be a single object or list of REST-format objects.
5259
*/
5360
expandFilesInObject(config, object) {
5461
if (object instanceof Array) {
55-
object.map(obj => this.expandFilesInObject(config, obj));
62+
object.map((obj) => this.expandFilesInObject(config, obj));
5663
return;
5764
}
5865
if (typeof object !== 'object') {

0 commit comments

Comments
 (0)