client/api: merge URL and Blob based file uploads
This commit is contained in:
parent
036fa9ee39
commit
be6f8d7f46
|
@ -63,6 +63,7 @@ class Api extends events.EventTarget {
|
||||||
|
|
||||||
_process(url, requestFactory, data, files, options) {
|
_process(url, requestFactory, data, files, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
data = Object.assign({}, data);
|
||||||
const [fullUrl, query] = this._getFullUrl(url);
|
const [fullUrl, query] = this._getFullUrl(url);
|
||||||
|
|
||||||
let abortFunction = null;
|
let abortFunction = null;
|
||||||
|
@ -74,14 +75,19 @@ class Api extends events.EventTarget {
|
||||||
if (query) {
|
if (query) {
|
||||||
req.query(query);
|
req.query(query);
|
||||||
}
|
}
|
||||||
if (data) {
|
|
||||||
req.attach('metadata', new Blob([JSON.stringify(data)]));
|
|
||||||
}
|
|
||||||
if (files) {
|
if (files) {
|
||||||
for (let key of Object.keys(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 {
|
try {
|
||||||
if (this.userName && this.userPassword) {
|
if (this.userName && this.userPassword) {
|
||||||
req.auth(
|
req.auth(
|
||||||
|
|
|
@ -85,9 +85,8 @@ class PostUploadController {
|
||||||
let reverseSearchPromise = Promise.resolve();
|
let reverseSearchPromise = Promise.resolve();
|
||||||
if (!uploadable.lookalikesConfirmed &&
|
if (!uploadable.lookalikesConfirmed &&
|
||||||
['image'].includes(uploadable.type)) {
|
['image'].includes(uploadable.type)) {
|
||||||
reverseSearchPromise = uploadable.url ?
|
reverseSearchPromise =
|
||||||
Post.reverseSearchFromUrl(uploadable.url) :
|
Post.reverseSearch(uploadable.url || uploadable.file);
|
||||||
Post.reverseSearchFromFile(uploadable.file);
|
|
||||||
}
|
}
|
||||||
this._lastCancellablePromise = reverseSearchPromise;
|
this._lastCancellablePromise = reverseSearchPromise;
|
||||||
|
|
||||||
|
@ -140,11 +139,7 @@ class PostUploadController {
|
||||||
post.flags = uploadable.flags;
|
post.flags = uploadable.flags;
|
||||||
post.tags = uploadable.tags;
|
post.tags = uploadable.tags;
|
||||||
post.relations = uploadable.relations;
|
post.relations = uploadable.relations;
|
||||||
if (uploadable.url) {
|
post.newContent = uploadable.url || uploadable.file;
|
||||||
post.newContentUrl = uploadable.url;
|
|
||||||
} else {
|
|
||||||
post.newContent = uploadable.file;
|
|
||||||
}
|
|
||||||
return post;
|
return post;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,6 @@ class Post extends events.EventTarget {
|
||||||
get canvasHeight() { return this._canvasHeight || 450; }
|
get canvasHeight() { return this._canvasHeight || 450; }
|
||||||
get fileSize() { return this._fileSize || 0; }
|
get fileSize() { return this._fileSize || 0; }
|
||||||
get newContent() { throw 'Invalid operation'; }
|
get newContent() { throw 'Invalid operation'; }
|
||||||
get newContentUrl() { throw 'Invalid operation'; }
|
|
||||||
get newThumbnail() { throw 'Invalid operation'; }
|
get newThumbnail() { throw 'Invalid operation'; }
|
||||||
|
|
||||||
get flags() { return this._flags; }
|
get flags() { return this._flags; }
|
||||||
|
@ -60,7 +59,6 @@ class Post extends events.EventTarget {
|
||||||
set safety(value) { this._safety = value; }
|
set safety(value) { this._safety = value; }
|
||||||
set relations(value) { this._relations = value; }
|
set relations(value) { this._relations = value; }
|
||||||
set newContent(value) { this._newContent = value; }
|
set newContent(value) { this._newContent = value; }
|
||||||
set newContentUrl(value) { this._newContentUrl = value; }
|
|
||||||
set newThumbnail(value) { this._newThumbnail = value; }
|
set newThumbnail(value) { this._newThumbnail = value; }
|
||||||
|
|
||||||
static fromResponse(response) {
|
static fromResponse(response) {
|
||||||
|
@ -69,16 +67,9 @@ class Post extends events.EventTarget {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static reverseSearchFromFile(content) {
|
static reverseSearch(content) {
|
||||||
return Post._reverseSearch({content: content});
|
let apiPromise = api.post(
|
||||||
}
|
'/posts/reverse-search', {}, {content: content});
|
||||||
|
|
||||||
static reverseSearchFromUrl(imageUrl) {
|
|
||||||
return Post._reverseSearch({contentUrl: imageUrl});
|
|
||||||
}
|
|
||||||
|
|
||||||
static _reverseSearch(request) {
|
|
||||||
let apiPromise = api.post('/posts/reverse-search', {}, request);
|
|
||||||
let returnedPromise = apiPromise
|
let returnedPromise = apiPromise
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (response.exactPost) {
|
if (response.exactPost) {
|
||||||
|
@ -132,7 +123,7 @@ class Post extends events.EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
save(anonymous) {
|
save(anonymous) {
|
||||||
const files = [];
|
const files = {};
|
||||||
const detail = {version: this._version};
|
const detail = {version: this._version};
|
||||||
|
|
||||||
// send only changed fields to avoid user privilege violation
|
// send only changed fields to avoid user privilege violation
|
||||||
|
@ -160,8 +151,6 @@ class Post extends events.EventTarget {
|
||||||
}
|
}
|
||||||
if (this._newContent) {
|
if (this._newContent) {
|
||||||
files.content = this._newContent;
|
files.content = this._newContent;
|
||||||
} else if (this._newContentUrl) {
|
|
||||||
detail.contentUrl = this._newContentUrl;
|
|
||||||
}
|
}
|
||||||
if (this._newThumbnail !== undefined) {
|
if (this._newThumbnail !== undefined) {
|
||||||
files.thumbnail = this._newThumbnail;
|
files.thumbnail = this._newThumbnail;
|
||||||
|
@ -175,7 +164,7 @@ class Post extends events.EventTarget {
|
||||||
this._updateFromResponse(response);
|
this._updateFromResponse(response);
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new CustomEvent('change', {detail: {post: this}}));
|
new CustomEvent('change', {detail: {post: this}}));
|
||||||
if (this._newContent || this._newContentUrl) {
|
if (this._newContent) {
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new CustomEvent('changeContent', {detail: {post: this}}));
|
new CustomEvent('changeContent', {detail: {post: this}}));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue