|
3 | 3 | // Stores files in Mongo using GridStore |
4 | 4 | // Requires the database adapter to be based on mongoclient |
5 | 5 |
|
6 | | -var GridStore = require('mongodb').GridStore; |
7 | | -var path = require('path'); |
| 6 | +import { GridStore } from 'mongodb'; |
8 | 7 |
|
9 | | -// For a given config object, filename, and data, store a file |
10 | | -// Returns a promise |
11 | | -function create(config, filename, data) { |
12 | | - return config.database.connect().then(() => { |
13 | | - var gridStore = new GridStore(config.database.db, filename, 'w'); |
14 | | - return gridStore.open(); |
15 | | - }).then((gridStore) => { |
16 | | - return gridStore.write(data); |
17 | | - }).then((gridStore) => { |
18 | | - return gridStore.close(); |
19 | | - }); |
20 | | -} |
| 8 | +import * as Path from 'path'; |
| 9 | +import { FilesAdapter } from './FilesAdapter'; |
21 | 10 |
|
22 | | -// Search for and return a file if found by filename |
23 | | -// Resolves a promise that succeeds with the buffer result |
24 | | -// from GridStore |
25 | | -function get(config, filename) { |
26 | | - return config.database.connect().then(() => { |
27 | | - return GridStore.exist(config.database.db, filename); |
28 | | - }).then(() => { |
29 | | - var gridStore = new GridStore(config.database.db, filename, 'r'); |
30 | | - return gridStore.open(); |
31 | | - }).then((gridStore) => { |
32 | | - return gridStore.read(); |
33 | | - }); |
34 | | -} |
| 11 | +class GridStoreAdapter extends FilesAdapter { |
| 12 | + // For a given config object, filename, and data, store a file |
| 13 | + // Returns a promise |
| 14 | + createFileAsync(config, filename, data) { |
| 15 | + return config.database.connect().then(() => { |
| 16 | + let gridStore = new GridStore(config.database.db, filename, 'w'); |
| 17 | + return gridStore.open(); |
| 18 | + }).then((gridStore) => { |
| 19 | + return gridStore.write(data); |
| 20 | + }).then((gridStore) => { |
| 21 | + return gridStore.close(); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + getFileDataAsync(config, filename) { |
| 26 | + return config.database.connect().then(() => { |
| 27 | + return GridStore.exist(config.database.db, filename); |
| 28 | + }).then(() => { |
| 29 | + let gridStore = new GridStore(config.database.db, filename, 'r'); |
| 30 | + return gridStore.open(); |
| 31 | + }).then((gridStore) => { |
| 32 | + return gridStore.read(); |
| 33 | + }); |
| 34 | + } |
35 | 35 |
|
36 | | -// Generates and returns the location of a file stored in GridStore for the |
37 | | -// given request and filename |
38 | | -function location(config, req, filename) { |
39 | | - return (req.protocol + '://' + req.get('host') + |
40 | | - path.dirname(req.originalUrl) + '/' + req.config.applicationId + |
| 36 | + getFileLocation(config, request, filename) { |
| 37 | + return (request.protocol + '://' + request.get('host') + |
| 38 | + Path.dirname(request.originalUrl) + '/' + config.applicationId + |
41 | 39 | '/' + encodeURIComponent(filename)); |
| 40 | + } |
42 | 41 | } |
43 | 42 |
|
44 | | -module.exports = { |
45 | | - create: create, |
46 | | - get: get, |
47 | | - location: location |
48 | | -}; |
| 43 | +export default GridStoreAdapter; |
0 commit comments