1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var s3 = require("s3");
|
7 |
|
8 | var Model = function( options ){
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | this.options = options;
|
15 |
|
16 | var config = this.options.store;
|
17 | this.store = s3.createClient({
|
18 |
|
19 |
|
20 | s3Options: {
|
21 | accessKeyId: config.key,
|
22 | secretAccessKey: config.secret,
|
23 | region: config.region
|
24 |
|
25 | }
|
26 | });
|
27 | }
|
28 |
|
29 |
|
30 | Model.prototype = {
|
31 |
|
32 | constructor: Model,
|
33 |
|
34 | create: function( data, callback ){
|
35 |
|
36 | var options = {};
|
37 |
|
38 | var params = {
|
39 | localFile: data.source,
|
40 | s3Params: {
|
41 | Bucket: this.options.store.bucket,
|
42 | Key: data.destination,
|
43 | ACL: this.options.store.acl || 'private'
|
44 |
|
45 |
|
46 |
|
47 | },
|
48 | };
|
49 |
|
50 | if( this.options.permissions == "public" ){
|
51 |
|
52 | options['ACL'] = 'public-read';
|
53 |
|
54 | }
|
55 |
|
56 |
|
57 | var uploader = this.store.uploadFile( params );
|
58 |
|
59 | uploader.on('error', function(err) {
|
60 | console.error("unable to upload:", err.stack);
|
61 | });
|
62 | uploader.on('progress', function() {
|
63 |
|
64 | });
|
65 | uploader.on('end', function() {
|
66 |
|
67 | callback( true );
|
68 | });
|
69 | },
|
70 |
|
71 | read: function( query, callback ){
|
72 | var key = query.access_token || query.code || false;
|
73 | if( !key ) return callback(null, false);
|
74 |
|
75 | this.store.get( key, function(err, data){
|
76 | if(err) return callback(err);
|
77 |
|
78 | data = JSON.parse( data.toString() );
|
79 | callback( null, data );
|
80 | });
|
81 | },
|
82 |
|
83 | destroy: function( item, callback ){
|
84 | var key = query.access_token || query.code || false;
|
85 | if( !key ) return callback(null, false);
|
86 |
|
87 | this.store.del( key, function(err, data){
|
88 | if(err) return callback(err);
|
89 | callback( null, true );
|
90 | });
|
91 | }
|
92 |
|
93 | }
|
94 |
|
95 |
|
96 | module.exports = Model; |
\ | No newline at end of file |