From ac321fa62a6183ad339974e24a48ea550c881d8b Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Fri, 22 Feb 2019 12:29:40 +0700 Subject: [PATCH] Updated /images* response structure refs #10438 --- ghost/admin/app/components/gh-image-uploader.js | 3 ++- ghost/admin/app/components/gh-uploader.js | 6 +++--- ghost/admin/app/controllers/setup/two.js | 5 +++-- ghost/admin/app/controllers/signup.js | 5 +++-- ghost/admin/mirage/config/uploads.js | 4 +++- .../tests/integration/components/gh-file-uploader-test.js | 6 +++--- .../tests/integration/components/gh-image-uploader-test.js | 2 +- .../admin/tests/integration/components/gh-uploader-test.js | 2 +- 8 files changed, 19 insertions(+), 14 deletions(-) diff --git a/ghost/admin/app/components/gh-image-uploader.js b/ghost/admin/app/components/gh-image-uploader.js index eefb7dd265..87fa69c02a 100644 --- a/ghost/admin/app/components/gh-image-uploader.js +++ b/ghost/admin/app/components/gh-image-uploader.js @@ -7,6 +7,7 @@ import { isVersionMismatchError } from 'ghost-admin/services/ajax'; import {computed} from '@ember/object'; +import {get} from '@ember/object'; import {htmlSafe} from '@ember/string'; import {isBlank} from '@ember/utils'; import {isArray as isEmberArray} from '@ember/array'; @@ -245,7 +246,7 @@ export default Component.extend({ return xhr; } }).then((response) => { - let url = JSON.parse(response); + let url = get(JSON.parse(response), 'url'); this._uploadSuccess(url); }).catch((error) => { this._uploadFailed(error); diff --git a/ghost/admin/app/components/gh-uploader.js b/ghost/admin/app/components/gh-uploader.js index 912434a073..465887e8ba 100644 --- a/ghost/admin/app/components/gh-uploader.js +++ b/ghost/admin/app/components/gh-uploader.js @@ -2,6 +2,7 @@ import Component from '@ember/component'; import EmberObject from '@ember/object'; import ghostPaths from 'ghost-admin/utils/ghost-paths'; import {all, task} from 'ember-concurrency'; +import {get} from '@ember/object'; import {isArray as isEmberArray} from '@ember/array'; import {isEmpty} from '@ember/utils'; import {run} from '@ember/runloop'; @@ -239,11 +240,10 @@ export default Component.extend({ tracker.update({loaded: file.size, total: file.size}); this._updateProgress(); - // TODO: is it safe to assume we'll only get a url back? - let uploadUrl = JSON.parse(response); + let uploadResponse = JSON.parse(response); let result = { fileName: file.name, - url: uploadUrl + url: get(uploadResponse, 'url') }; this.get('uploadUrls')[index] = result; diff --git a/ghost/admin/app/controllers/setup/two.js b/ghost/admin/app/controllers/setup/two.js index a14c54d870..18e61c9e5c 100644 --- a/ghost/admin/app/controllers/setup/two.js +++ b/ghost/admin/app/controllers/setup/two.js @@ -2,6 +2,7 @@ import Controller, {inject as controller} from '@ember/controller'; import RSVP from 'rsvp'; import ValidationEngine from 'ghost-admin/mixins/validation-engine'; +import {get} from '@ember/object'; import {isInvalidError} from 'ember-ajax/errors'; import {isVersionMismatchError} from 'ghost-admin/services/ajax'; import {inject as service} from '@ember/service'; @@ -85,7 +86,7 @@ export default Controller.extend(ValidationEngine, { _sendImage(user) { let formData = new FormData(); let imageFile = this.get('profileImage'); - let uploadUrl = this.get('ghostPaths.url').api('uploads'); + let uploadUrl = this.get('ghostPaths.url').api('images'); formData.append('uploadimage', imageFile, imageFile.name); @@ -95,7 +96,7 @@ export default Controller.extend(ValidationEngine, { contentType: false, dataType: 'text' }).then((response) => { - let imageUrl = JSON.parse(response); + let imageUrl = get(JSON.parse(response), 'url'); let usersUrl = this.get('ghostPaths.url').api('users', user.id.toString()); user.profile_image = imageUrl; diff --git a/ghost/admin/app/controllers/signup.js b/ghost/admin/app/controllers/signup.js index d78898e0e3..fb2a01baea 100644 --- a/ghost/admin/app/controllers/signup.js +++ b/ghost/admin/app/controllers/signup.js @@ -1,5 +1,6 @@ import Controller from '@ember/controller'; import {alias} from '@ember/object/computed'; +import {get} from '@ember/object'; import {isArray as isEmberArray} from '@ember/array'; import { isVersionMismatchError @@ -98,7 +99,7 @@ export default Controller.extend({ _sendImage: task(function* () { let formData = new FormData(); let imageFile = this.get('profileImage'); - let uploadUrl = this.get('ghostPaths.url').api('uploads'); + let uploadUrl = this.get('ghostPaths.url').api('images'); if (imageFile) { formData.append('uploadimage', imageFile, imageFile.name); @@ -111,7 +112,7 @@ export default Controller.extend({ dataType: 'text' }); - let imageUrl = JSON.parse(response); + let imageUrl = get(JSON.parse(response), 'url'); let usersUrl = this.get('ghostPaths.url').api('users', user.id.toString()); user.profile_image = imageUrl; diff --git a/ghost/admin/mirage/config/uploads.js b/ghost/admin/mirage/config/uploads.js index 6d1bb213fd..c05ae164f4 100644 --- a/ghost/admin/mirage/config/uploads.js +++ b/ghost/admin/mirage/config/uploads.js @@ -8,7 +8,9 @@ const fileUploadResponse = function (db, {requestBody}) { month = `0${month}`; } - return `"/content/images/${year}/${month}/${file.name}"`; + return { + url: `/content/images/${year}/${month}/${file.name}` + }; }; export default function mockUploads(server) { diff --git a/ghost/admin/tests/integration/components/gh-file-uploader-test.js b/ghost/admin/tests/integration/components/gh-file-uploader-test.js index 92bc17ad35..75d9a908ae 100644 --- a/ghost/admin/tests/integration/components/gh-file-uploader-test.js +++ b/ghost/admin/tests/integration/components/gh-file-uploader-test.js @@ -19,7 +19,7 @@ const notificationsStub = Service.extend({ const stubSuccessfulUpload = function (server, delay = 0) { server.post('/ghost/api/v2/admin/images/', function () { - return [200, {'Content-Type': 'application/json'}, '"/content/images/test.png"']; + return [200, {'Content-Type': 'application/json'}, '{"url":"/content/images/test.png"}']; }, delay); }; @@ -99,7 +99,7 @@ describe('Integration: Component: gh-file-uploader', function () { await fileUpload('input[type="file"]', ['test'], {name: 'test.csv'}); expect(uploadSuccess.calledOnce).to.be.true; - expect(uploadSuccess.firstCall.args[0]).to.equal('/content/images/test.png'); + expect(uploadSuccess.firstCall.args[0]).to.eql({url: '/content/images/test.png'}); }); it('doesn\'t fire uploadSuccess action on failed upload', async function () { @@ -313,7 +313,7 @@ describe('Integration: Component: gh-file-uploader', function () { await settled(); expect(uploadSuccess.calledOnce).to.be.true; - expect(uploadSuccess.firstCall.args[0]).to.equal('/content/images/test.png'); + expect(uploadSuccess.firstCall.args[0]).to.eql({url: '/content/images/test.png'}); }); it('validates extension by default', async function () { diff --git a/ghost/admin/tests/integration/components/gh-image-uploader-test.js b/ghost/admin/tests/integration/components/gh-image-uploader-test.js index 2524689fe8..8d81120dd5 100644 --- a/ghost/admin/tests/integration/components/gh-image-uploader-test.js +++ b/ghost/admin/tests/integration/components/gh-image-uploader-test.js @@ -30,7 +30,7 @@ const sessionStub = Service.extend({ const stubSuccessfulUpload = function (server, delay = 0) { server.post('/ghost/api/v2/admin/images/', function () { - return [200, {'Content-Type': 'application/json'}, '"/content/images/test.png"']; + return [200, {'Content-Type': 'application/json'}, '{"url":"/content/images/test.png"}']; }, delay); }; diff --git a/ghost/admin/tests/integration/components/gh-uploader-test.js b/ghost/admin/tests/integration/components/gh-uploader-test.js index 057ba7e3b8..e74d0b20a3 100644 --- a/ghost/admin/tests/integration/components/gh-uploader-test.js +++ b/ghost/admin/tests/integration/components/gh-uploader-test.js @@ -10,7 +10,7 @@ import {setupRenderingTest} from 'ember-mocha'; const stubSuccessfulUpload = function (server, delay = 0) { server.post('/ghost/api/v2/admin/images/', function () { - return [200, {'Content-Type': 'application/json'}, '"/content/images/test.png"']; + return [200, {'Content-Type': 'application/json'}, '{"url": "/content/images/test.png"}']; }, delay); };