From be6f8d7f465dd75932b77e5f8d6a7a18209ded8b Mon Sep 17 00:00:00 2001 From: rr- Date: Sat, 7 Jan 2017 12:33:17 +0100 Subject: [PATCH] client/api: merge URL and Blob based file uploads --- client/js/api.js | 14 +++++++++---- .../js/controllers/post_upload_controller.js | 11 +++------- client/js/models/post.js | 21 +++++-------------- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/client/js/api.js b/client/js/api.js index fe0f4ed..740051a 100644 --- a/client/js/api.js +++ b/client/js/api.js @@ -63,6 +63,7 @@ class Api extends events.EventTarget { _process(url, requestFactory, data, files, options) { options = options || {}; + data = Object.assign({}, data); const [fullUrl, query] = this._getFullUrl(url); let abortFunction = null; @@ -74,14 +75,19 @@ class Api extends events.EventTarget { if (query) { req.query(query); } - if (data) { - req.attach('metadata', new Blob([JSON.stringify(data)])); - } if (files) { for (let key of Object.keys(files)) { - req.attach(key, files[key] || new Blob()); + const value = files[key]; + if (value.constructor === String) { + data[key + 'Url'] = value; + } else { + req.attach(key, value || new Blob()); + } } } + if (data) { + req.attach('metadata', new Blob([JSON.stringify(data)])); + } try { if (this.userName && this.userPassword) { req.auth( diff --git a/client/js/controllers/post_upload_controller.js b/client/js/controllers/post_upload_controller.js index ca40741..a605a42 100644 --- a/client/js/controllers/post_upload_controller.js +++ b/client/js/controllers/post_upload_controller.js @@ -85,9 +85,8 @@ class PostUploadController { let reverseSearchPromise = Promise.resolve(); if (!uploadable.lookalikesConfirmed && ['image'].includes(uploadable.type)) { - reverseSearchPromise = uploadable.url ? - Post.reverseSearchFromUrl(uploadable.url) : - Post.reverseSearchFromFile(uploadable.file); + reverseSearchPromise = + Post.reverseSearch(uploadable.url || uploadable.file); } this._lastCancellablePromise = reverseSearchPromise; @@ -140,11 +139,7 @@ class PostUploadController { post.flags = uploadable.flags; post.tags = uploadable.tags; post.relations = uploadable.relations; - if (uploadable.url) { - post.newContentUrl = uploadable.url; - } else { - post.newContent = uploadable.file; - } + post.newContent = uploadable.url || uploadable.file; return post; } } diff --git a/client/js/models/post.js b/client/js/models/post.js index 18a6223..381c32f 100644 --- a/client/js/models/post.js +++ b/client/js/models/post.js @@ -39,7 +39,6 @@ class Post extends events.EventTarget { get canvasHeight() { return this._canvasHeight || 450; } get fileSize() { return this._fileSize || 0; } get newContent() { throw 'Invalid operation'; } - get newContentUrl() { throw 'Invalid operation'; } get newThumbnail() { throw 'Invalid operation'; } get flags() { return this._flags; } @@ -60,7 +59,6 @@ class Post extends events.EventTarget { set safety(value) { this._safety = value; } set relations(value) { this._relations = value; } set newContent(value) { this._newContent = value; } - set newContentUrl(value) { this._newContentUrl = value; } set newThumbnail(value) { this._newThumbnail = value; } static fromResponse(response) { @@ -69,16 +67,9 @@ class Post extends events.EventTarget { return ret; } - static reverseSearchFromFile(content) { - return Post._reverseSearch({content: content}); - } - - static reverseSearchFromUrl(imageUrl) { - return Post._reverseSearch({contentUrl: imageUrl}); - } - - static _reverseSearch(request) { - let apiPromise = api.post('/posts/reverse-search', {}, request); + static reverseSearch(content) { + let apiPromise = api.post( + '/posts/reverse-search', {}, {content: content}); let returnedPromise = apiPromise .then(response => { if (response.exactPost) { @@ -132,7 +123,7 @@ class Post extends events.EventTarget { } save(anonymous) { - const files = []; + const files = {}; const detail = {version: this._version}; // send only changed fields to avoid user privilege violation @@ -160,8 +151,6 @@ class Post extends events.EventTarget { } if (this._newContent) { files.content = this._newContent; - } else if (this._newContentUrl) { - detail.contentUrl = this._newContentUrl; } if (this._newThumbnail !== undefined) { files.thumbnail = this._newThumbnail; @@ -175,7 +164,7 @@ class Post extends events.EventTarget { this._updateFromResponse(response); this.dispatchEvent( new CustomEvent('change', {detail: {post: this}})); - if (this._newContent || this._newContentUrl) { + if (this._newContent) { this.dispatchEvent( new CustomEvent('changeContent', {detail: {post: this}})); }