@@ -13,7 +13,7 @@
|
||||
// and for the API server to be using the DatabaseController with Mongo
|
||||
// database adapter.
|
||||
|
||||
import type { Config } from '../../Config'
|
||||
import type { Config } from '../../Config';
|
||||
/**
|
||||
* @module Adapters
|
||||
*/
|
||||
@@ -21,7 +21,6 @@ import type { Config } from '../../Config'
|
||||
* @interface FilesAdapter
|
||||
*/
|
||||
export class FilesAdapter {
|
||||
|
||||
/** Responsible for storing the file in order to be retrieved later by its filename
|
||||
*
|
||||
* @param {string} filename - the filename to save
|
||||
@@ -31,7 +30,7 @@ export class FilesAdapter {
|
||||
*
|
||||
* @return {Promise} a promise that should fail if the storage didn't succeed
|
||||
*/
|
||||
createFile(filename: string, data, contentType: string): Promise { }
|
||||
createFile(filename: string, data, contentType: string): Promise {}
|
||||
|
||||
/** Responsible for deleting the specified file
|
||||
*
|
||||
@@ -39,7 +38,7 @@ export class FilesAdapter {
|
||||
*
|
||||
* @return {Promise} a promise that should fail if the deletion didn't succeed
|
||||
*/
|
||||
deleteFile(filename: string): Promise { }
|
||||
deleteFile(filename: string): Promise {}
|
||||
|
||||
/** Responsible for retrieving the data of the specified file
|
||||
*
|
||||
@@ -47,7 +46,7 @@ export class FilesAdapter {
|
||||
*
|
||||
* @return {Promise} a promise that should pass with the file data or fail on error
|
||||
*/
|
||||
getFileData(filename: string): Promise<any> { }
|
||||
getFileData(filename: string): Promise<any> {}
|
||||
|
||||
/** Returns an absolute URL where the file can be accessed
|
||||
*
|
||||
@@ -56,7 +55,7 @@ export class FilesAdapter {
|
||||
*
|
||||
* @return {string} Absolute URL
|
||||
*/
|
||||
getFileLocation(config: Config, filename: string): string { }
|
||||
getFileLocation(config: Config, filename: string): string {}
|
||||
}
|
||||
|
||||
export default FilesAdapter;
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
*/
|
||||
|
||||
// @flow-disable-next
|
||||
import { MongoClient, GridStore, Db} from 'mongodb';
|
||||
import { FilesAdapter } from './FilesAdapter';
|
||||
import defaults from '../../defaults';
|
||||
import { MongoClient, GridStore, Db } from 'mongodb';
|
||||
import { FilesAdapter } from './FilesAdapter';
|
||||
import defaults from '../../defaults';
|
||||
|
||||
export class GridStoreAdapter extends FilesAdapter {
|
||||
_databaseURI: string;
|
||||
@@ -22,8 +22,9 @@ export class GridStoreAdapter extends FilesAdapter {
|
||||
|
||||
_connect() {
|
||||
if (!this._connectionPromise) {
|
||||
this._connectionPromise = MongoClient.connect(this._databaseURI)
|
||||
.then((client) => client.db(client.s.options.dbName));
|
||||
this._connectionPromise = MongoClient.connect(this._databaseURI).then(
|
||||
client => client.db(client.s.options.dbName)
|
||||
);
|
||||
}
|
||||
return this._connectionPromise;
|
||||
}
|
||||
@@ -31,41 +32,54 @@ export class GridStoreAdapter extends FilesAdapter {
|
||||
// For a given config object, filename, and data, store a file
|
||||
// Returns a promise
|
||||
createFile(filename: string, data) {
|
||||
return this._connect().then((database) => {
|
||||
const gridStore = new GridStore(database, filename, 'w');
|
||||
return gridStore.open();
|
||||
}).then(gridStore => {
|
||||
return gridStore.write(data);
|
||||
}).then(gridStore => {
|
||||
return gridStore.close();
|
||||
});
|
||||
return this._connect()
|
||||
.then(database => {
|
||||
const gridStore = new GridStore(database, filename, 'w');
|
||||
return gridStore.open();
|
||||
})
|
||||
.then(gridStore => {
|
||||
return gridStore.write(data);
|
||||
})
|
||||
.then(gridStore => {
|
||||
return gridStore.close();
|
||||
});
|
||||
}
|
||||
|
||||
deleteFile(filename: string) {
|
||||
return this._connect().then(database => {
|
||||
const gridStore = new GridStore(database, filename, 'r');
|
||||
return gridStore.open();
|
||||
}).then((gridStore) => {
|
||||
return gridStore.unlink();
|
||||
}).then((gridStore) => {
|
||||
return gridStore.close();
|
||||
});
|
||||
return this._connect()
|
||||
.then(database => {
|
||||
const gridStore = new GridStore(database, filename, 'r');
|
||||
return gridStore.open();
|
||||
})
|
||||
.then(gridStore => {
|
||||
return gridStore.unlink();
|
||||
})
|
||||
.then(gridStore => {
|
||||
return gridStore.close();
|
||||
});
|
||||
}
|
||||
|
||||
getFileData(filename: string) {
|
||||
return this._connect().then(database => {
|
||||
return GridStore.exist(database, filename)
|
||||
.then(() => {
|
||||
return this._connect()
|
||||
.then(database => {
|
||||
return GridStore.exist(database, filename).then(() => {
|
||||
const gridStore = new GridStore(database, filename, 'r');
|
||||
return gridStore.open();
|
||||
});
|
||||
}).then(gridStore => {
|
||||
return gridStore.read();
|
||||
});
|
||||
})
|
||||
.then(gridStore => {
|
||||
return gridStore.read();
|
||||
});
|
||||
}
|
||||
|
||||
getFileLocation(config, filename) {
|
||||
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
|
||||
return (
|
||||
config.mount +
|
||||
'/files/' +
|
||||
config.applicationId +
|
||||
'/' +
|
||||
encodeURIComponent(filename)
|
||||
);
|
||||
}
|
||||
|
||||
getFileStream(filename: string) {
|
||||
|
||||
Reference in New Issue
Block a user