Files
kami-parse-server/src/Adapters/Files/GridStoreAdapter.js

51 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-01-28 10:58:12 -08:00
// GridStoreAdapter
//
// Stores files in Mongo using GridStore
// Requires the database adapter to be based on mongoclient
2016-02-08 22:51:58 -08:00
import { GridStore } from 'mongodb';
import { FilesAdapter } from './FilesAdapter';
2016-01-28 10:58:12 -08:00
export class GridStoreAdapter extends FilesAdapter {
2016-02-08 22:51:58 -08:00
// For a given config object, filename, and data, store a file
// Returns a promise
createFile(config, filename, data) {
2016-02-08 22:51:58 -08:00
return config.database.connect().then(() => {
let gridStore = new GridStore(config.database.adapter.database, filename, 'w');
2016-02-08 22:51:58 -08:00
return gridStore.open();
}).then((gridStore) => {
return gridStore.write(data);
}).then((gridStore) => {
return gridStore.close();
});
}
2016-02-10 18:42:21 -05:00
deleteFile(config, filename) {
return config.database.connect().then(() => {
let gridStore = new GridStore(config.database.adapter.database, filename, 'w');
2016-02-10 18:42:21 -05:00
return gridStore.open();
}).then((gridStore) => {
return gridStore.unlink();
}).then((gridStore) => {
return gridStore.close();
});
}
getFileData(config, filename) {
2016-02-08 22:51:58 -08:00
return config.database.connect().then(() => {
return GridStore.exist(config.database.adapter.database, filename);
2016-02-08 22:51:58 -08:00
}).then(() => {
let gridStore = new GridStore(config.database.adapter.database, filename, 'r');
2016-02-08 22:51:58 -08:00
return gridStore.open();
}).then((gridStore) => {
return gridStore.read();
});
}
2016-01-28 10:58:12 -08:00
getFileLocation(config, filename) {
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
2016-02-08 22:51:58 -08:00
}
2016-01-31 22:27:33 -08:00
}
2016-02-08 22:51:58 -08:00
export default GridStoreAdapter;