Skip to content

Commit 0220766

Browse files
committed
fixes w/ profile photo cut
1 parent 9d60f2a commit 0220766

8 files changed

Lines changed: 36 additions & 26 deletions

File tree

client/head/navigationArrows.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ var ctrlOrAlt = ~navigator.userAgent.toLowerCase().indexOf("mac os x") ? 'ctrl'
44

55
function onKeyDown(event) {
66
// don't react on Ctrl-> <- if in text
7-
if (~['INPUT', 'TEXTAREA', 'SELECT'].indexOf(document.activeElement.tagName)) return;
7+
if (document.activeElement) {
8+
if (~['INPUT', 'TEXTAREA', 'SELECT'].indexOf(document.activeElement.tagName)) return;
9+
}
810

911
if (!event[ctrlOrAlt + 'Key']) return;
1012

client/head/sidebar.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ function onClick(event) {
2929

3030
function onKeyDown(event) {
3131
// don't react on Ctrl-> <- if in text
32-
if (~['INPUT', 'TEXTAREA', 'SELECT'].indexOf(document.activeElement.tagName)) return;
32+
if (document.activeElement) {
33+
if (~['INPUT', 'TEXTAREA', 'SELECT'].indexOf(document.activeElement.tagName)) return;
34+
}
3335

3436
if (event.keyCode != "S".charCodeAt(0)) return;
3537

handlers/profile/client/directive/profilePhoto.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ angular.module('profile')
2222
fileInput.accept = "image/*";
2323

2424
fileInput.onchange = function() {
25-
25+
fileInput.remove();
2626
var reader = new FileReader();
2727
var file = fileInput.files[0];
2828

@@ -36,7 +36,6 @@ angular.module('profile')
3636
uploadPhoto(file);
3737
} else {
3838
cutPhoto(image, function(blob) {
39-
// TODO: server-side
4039
// @see http://stackoverflow.com/questions/13198131/how-to-save-a-html5-canvas-as-image-on-a-server
4140
// @see http://stackoverflow.com/questions/12391628/how-can-i-upload-an-embedded-image-with-javascript
4241
uploadPhoto(blob);
@@ -48,7 +47,12 @@ angular.module('profile')
4847
reader.readAsDataURL(file);
4948

5049
};
50+
51+
// must be in body for IE
52+
fileInput.hidden = true;
53+
document.body.appendChild(fileInput);
5154
fileInput.click();
55+
5256
};
5357

5458
function uploadPhoto(file) {

handlers/users/controllers/id.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ exports.del = function*(next) {
4949
};
5050

5151

52-
var readMultipart = thunkify(function(req, done) {
52+
var readMultipart = thunkify(function(ctx, done) {
53+
var req = ctx.req;
5354

5455
var hadError = false;
5556
var fields = {};
@@ -60,11 +61,14 @@ var readMultipart = thunkify(function(req, done) {
6061
var form = new multiparty.Form();
6162

6263
form.on('field', function(name, value) {
64+
ctx.log.debug("Field", name, value);
6365
fields[name] = value;
6466
});
6567

6668
// multipart file must be the last
6769
form.on('part', function(part) {
70+
ctx.log.debug("Part", part.name, part.filename);
71+
6872
// upload multipart to imgur, no other multipart items in the form
6973
if (part.name != 'photo') {
7074
return onError(new Error("Unexpected multipart field: " + part.name));
@@ -76,6 +80,7 @@ var readMultipart = thunkify(function(req, done) {
7680
}
7781

7882
co(function*() {
83+
// filename='blob' for FormData(photo, blob) where blob comes from canvas.toBlob
7984
return yield* imgur.uploadStream(part.filename, part.byteCount, part);
8085
}).then(function(result) {
8186
if (hadError) return;
@@ -113,7 +118,7 @@ exports.patch = function*(next) {
113118

114119
var fields;
115120
try {
116-
fields = yield readMultipart(this.req);
121+
fields = yield readMultipart(this);
117122
} catch (e) {
118123
if (e.name == 'BadImageError') {
119124
this.throw(400, e.message);

modules/imgur/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ exports.transload = function*(url) {
2929
}
3030

3131
return response.data;
32-
3332
};
3433

3534
exports.uploadBuffer = function*(fileName, buffer) {
@@ -55,6 +54,9 @@ custom_file: {
5554
*/
5655
exports.uploadStream = function*(fileName, knownLength, stream) {
5756

57+
if (!knownLength) {
58+
throw new BadImageError("Пустое изображение.");
59+
}
5860
var mimeType = mime.lookup(fileName);
5961

6062
var response = yield* imgurRequest('image', {

modules/photoCut/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ exports.cutPhoto = function(img, onSuccess) {
1818
selectionCanvas.width = selectionCanvas.offsetHeight;
1919
selectionCanvas.height = selectionCanvas.offsetWidth;
2020

21-
var photoCut = new PhotoCut(canvas);
21+
var photoCut = new PhotoCut(canvas, { maxImageSize: 300 });
2222
photoCut.setImage(img);
2323

2424
/*
@@ -51,30 +51,32 @@ exports.cutPhoto = function(img, onSuccess) {
5151

5252
modal.elem.querySelector('[data-action="done"]').addEventListener('click', () => {
5353

54-
modal.remove();
55-
5654
var selection = photoCut.getCanvasSelection();
5755

5856
if (!selection) return;
5957

6058
// resize canvas to the actual selection part of the image,
6159
// to make as large as possible resolution/size avatar
62-
selectionCanvas.width = selection.width;
63-
selectionCanvas.height = selection.height;
60+
selectionCanvas.width = selection.size;
61+
selectionCanvas.height = selection.size;
62+
6463

6564
// draw the selected piece on the canvas to make Blob of it
6665
selectionCanvas.getContext('2d').drawImage(
67-
selection.source, selection.x, selection.y, selection.width, selection.height,
68-
0, 0, selection.width, selection.height
66+
selection.source, selection.x, selection.y, selection.size, selection.size,
67+
0, 0, selection.size, selection.size
6968
);
7069

70+
modal.remove();
71+
7172
selectionCanvas.toBlob(
7273
function (blob) {
7374
onSuccess(blob);
7475
},
7576
'image/jpeg'
7677
);
7778

79+
7880
});
7981

8082
};

modules/photoCut/photoCut.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const CanvasSelection = require('./canvasSelection');
33

44
class PhotoCut {
55

6-
constructor(canvas) {
6+
constructor(canvas, {maxImageSize} = {}) {
7+
this.maxImageSize = maxImageSize || 200;
8+
79
this.canvas = canvas;
810

911
this.canvas.onmousedown = event => this.onMouseDown(event);
@@ -28,12 +30,11 @@ class PhotoCut {
2830

2931
setImage(img) {
3032
// fit into document & 400px
31-
var maxImageSize = Math.min(document.documentElement.clientHeight, document.documentElement.clientWidth, 300);
3233

3334
this.img = img;
3435

3536
// fit to canvas
36-
this.scale = Math.min(maxImageSize / img.width, maxImageSize / img.height);
37+
this.scale = Math.min(this.maxImageSize / img.width, this.maxImageSize / img.height);
3738

3839
this.fullImageCanvas = document.createElement('canvas');
3940
this.fullImageCtx = this.fullImageCanvas.getContext('2d');
@@ -125,7 +126,6 @@ class PhotoCut {
125126
onMouseMove(event) {
126127
// coords may be anywhere in the document
127128

128-
console.log("HERE!!!", event.clientX);
129129
// recalculate relative to canvas image edge
130130
var coords = this.getEventCoordsRelativeCanvasImage(event);
131131

@@ -135,9 +135,6 @@ class PhotoCut {
135135
if (coords.y < 0) coords.y = 0;
136136
if (coords.y > this.height) coords.y = this.height;
137137

138-
console.log(coords);
139-
140-
console.log(this.state);
141138
switch (this.state) {
142139
case false:
143140
this.showCursorAtCoords(coords);
@@ -176,8 +173,6 @@ class PhotoCut {
176173
coords.x > center.x && coords.y < center.y ? 'ne' :
177174
'se';
178175

179-
console.log(direction);
180-
181176
switch (direction) {
182177
case 'nw':
183178
this.selectionStartCoords = {
@@ -209,7 +204,6 @@ class PhotoCut {
209204
}
210205

211206
moveSelection(coords) {
212-
// console.log("moveSelection");
213207
var x = Math.min(coords.x - this.mouseDownShift.x, this.width - this.selection.size);
214208
var y = Math.min(coords.y - this.mouseDownShift.y, this.height - this.selection.size);
215209
if (x < 0) x = 0;
@@ -279,7 +273,6 @@ class PhotoCut {
279273
}
280274

281275
onMouseUp(event) {
282-
//console.log(event.type, event.clientX, event.clientY);
283276
if (!this.state) return;
284277
this.state = false;
285278

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"bunyan": "*",
3030
"co": "*",
3131
"css-loader": "*",
32-
"csv-parse": "^0.1.0",
32+
"csv-parse": "*",
3333
"del": "*",
3434
"docxtemplater": "*",
3535
"elasticsearch": "*",

0 commit comments

Comments
 (0)