forked from jamgold/cropuploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcropuploader-server.js
177 lines (170 loc) · 4.66 KB
/
cropuploader-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var Future = Npm.require("fibers/future");
Meteor.methods({
cropUploaderS3urlBase: function() {
return CropUploader.knox.urlBase;
},
cropUploaderS3contents: function(prefix) {
var future = new Future();
CropUploader.knox.list({prefix: prefix}, function(error, response) {
if(error)
{
console.log(error);
throw new Meteor.Error(500, "An error occured getting your files");
}
else
{
response.Contents.forEach(function(image){
image.urlBase = CropUploader.knox.urlBase;
});
future.return(response.Contents);
}
});
return future.wait();
},
cropUploaderS3Delete: function(url) {
// console.log(this.userId);
var future = new Future();
var uparts = url.split('//');
var purl = uparts.length > 2 ? uparts[2] : uparts[1];
var relativeUrl = '/'+purl.split('/').slice(1).join('/');
console.log('cropUploaderS3Delete', url, relativeUrl);
CropUploader.knox.deleteFile( relativeUrl, function(error, res) {
if(error) {
console.log('cropUploaderS3Delete', error);
future.return(true);
}
else future.return(true);
});
return future.wait()
},
cropUploaderConsolidate: function() {
var res = Meteor.call('cropUploaderS3contents',CropUploader.directory);
if(res)
{
res.forEach(function(image){
if( !image.Key.match(/\/$/) )
{
var url = 'https://'+image.urlBase+'/'+image.Key;
if( !image.Key.match('derivative/') )
{
var img = CropUploader.images.findOne({url: url});
if(!img)
{
console.log(url+' does not exist');
CropUploader.images.insert({
url: url,
relativeUrl: image.Key,
urlBase: image.urlBase,
});
}
else
{
if(img.relativeUrl == undefined)
{
CropUploader.images.update(img._id,{$set:{
relativeUrl: image.Key,
urlBase: image.urlBase,
}});
}
}
} else {
// slingshot/derivative/thumbnail/cb276bcc-3ee3-4cd8-a03b-d4965adfca71.1429836526914.png
var pp = image.Key.split('/');
var uuid = pp.pop().split('.')[0];
var img = CropUploader.images.findOne({uuid: uuid});
if(img)
{
var type = pp.pop();
// console.log(type, img.derivatives);
var set = {};set['derivatives.'+type] = url;
CropUploader.images.update(img._id,{$set: set});
}
else
{
console.log(uuid+' not adding '+image.Key);
}
}
}
});
}
}
});
Meteor.publish('cropUploaderImages', function(query) {
query = query || {};
return CropUploader.images.find(query);
});
CropUploader.images.allow({
insert: function(userId, image) {
// https://buzzledom-slingshot.s3-us-west-1.amazonaws.com/slingshot/2014-09-26.jpg
image.userId = userId;
image.created = new Date();
image.urlBase = CropUploader.knox.urlBase;
image.relativeUrl = image.url.split('//')[1].split('/').slice(1).join('/');
// if('insert' in CropUploader._hooks && typeof CropUploader._hooks.insert == 'function')
// image = CropUploader._hooks.insert(image);
return userId && image;
},
update: function(userId, doc, fieldNames, modifier) {
//
// check if we are updating a derivative and delete the old one first
//
// { '$set': { 'derivatives.thumbnail': 'https://buzzledom-slingshot.s3-us-west-1.amazonaws.com/slingshot/derivative/thumbnail3f2aae7d-711f-48d1-87fe-b58611014de0.1429835432477.png' } }
//
var allowed = doc.userId == userId;
var deleteUrl = null;
if( allowed && '$set' in modifier )
{
for(var m in modifier['$set'])
{
if(m.match('derivatives.'))
{
var t = m.split('.')[1];
var url = modifier['$set'][m];
if(url != doc.derivatives[t])
{
deleteUrl = url;
}
}
}
}
if(deleteUrl) {
console.log('images.update we should be deleting', deleteUrl);
}
return allowed;
},
remove: function(userId, doc) {
var future = new Future();
var files = [ doc.relativeUrl ];
if(doc.userId != userId) return false;
//
// build array of derivative relativeUrl
//
if(doc.derivatives)
for(var d in doc.derivatives) {
// add short relativeUrl
var uparts = doc.derivatives[d].split('//');
var purl = uparts.length > 2 ? uparts[2] : uparts[1];
var relativeUrl = '/'+purl.split('/').slice(1).join('/');
files.push( relativeUrl );
}
CropUploader.knox.deleteMultiple( files , function(error, res){
if(error)
{
console.log(error);
// throw new Meteor.Error(500, "An error occured deleting your file");
future.return(false);
}
else
{
future.return(true);
}
});
return future.wait();
}
});
Meteor.startup(function () {
//
// code to run on server at startup
//
// Meteor.call('cropUploaderConsolidate');
});