-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoto.js
More file actions
94 lines (76 loc) · 2.56 KB
/
Copy pathphoto.js
File metadata and controls
94 lines (76 loc) · 2.56 KB
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
var _ = require("underscore");
var Image = require("parse-image");
var resizePhotoAsync = function(photo) {
return Parse.Cloud.httpRequest({
url: photo.get("file").url()
}).then(function(response) {
var image = new Image();
return image.setData(response.buffer);
}).then(function(image) {
// Crop the image to the smaller of width or height.
var size = Math.min(image.width(), image.height());
return image.crop({
left: (image.width() - size) / 2,
top: (image.height() - size) / 2,
width: size,
height: size
});
}).then(function(image) {
// Resize the image to 200x200.
return image.scale({
width: 200,
height: 200
});
}).then(function(image) {
// Make sure it's a JPEG to save disk space and bandwidth.
return image.setFormat("JPEG");
}).then(function(image) {
// Get the image data in a Buffer.
return image.data();
}).then(function(buffer) {
// Save the image into a new file.
var base64 = buffer.toString("base64");
var cropped = new Parse.File("thumbnail200.jpg", { base64: base64 });
return cropped.save();
}).then(function(cropped) {
// Attach the image file to the original object.
photo.set("thumbnail200", cropped);
});
};
Parse.Cloud.beforeSave("Photo", function(request, response) {
if (!request.master && !request.user) {
response.error("Must be logged in.");
return;
}
var photo = request.object;
var babyUuid = photo.get("babyUuid");
var roleName = "Baby-" + babyUuid;
Parse.Promise.as().then(function() {
// Make sure that the photo is for a baby this user can write.
// Otherwise, people can write entries in other people's logs.
// This may be overkill, as guessing a baby's UUID is tantamount to
// (or even harder than) guessing their password. But it also makes sure
// the baby actually exists.
var roleQuery = new Parse.Query(Parse.Role);
roleQuery.equalTo("name", roleName);
return roleQuery.first({ useMasterKey: request.master });
}).then(function(role) {
if (!role) {
return Parse.Promise.error(
"This user does not have permission to make photos for this baby.");
}
var acl = new Parse.ACL();
acl.setRoleReadAccess(roleName, true);
acl.setRoleWriteAccess(roleName, true);
photo.setACL(acl);
// Make deleted be consistent.
if (photo.get("deleted") === null) {
photo.unset("deleted");
}
return resizePhotoAsync(photo);
}).then(function() {
response.success();
}, function(error) {
response.error(error);
});
});