From 0fb7a04baedd8470acf945d9b29a1f82147a0cfa Mon Sep 17 00:00:00 2001 From: Rishabh Date: Fri, 10 Dec 2021 18:01:25 +0530 Subject: [PATCH] Refined file upload card refs https://github.com/TryGhost/Team/issues/1231 - updated upload endpoint for files - cleanup --- .../addon/components/koenig-card-file.hbs | 6 +-- .../addon/components/koenig-card-file.js | 37 ++++++++----------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/ghost/admin/lib/koenig-editor/addon/components/koenig-card-file.hbs b/ghost/admin/lib/koenig-editor/addon/components/koenig-card-file.hbs index 68878e92ab..5538ce25a3 100644 --- a/ghost/admin/lib/koenig-editor/addon/components/koenig-card-file.hbs +++ b/ghost/admin/lib/koenig-editor/addon/components/koenig-card-file.hbs @@ -52,8 +52,8 @@ {{else}}
- Click to upload an audio file + Click to upload a file {{/if}} {{/if}} diff --git a/ghost/admin/lib/koenig-editor/addon/components/koenig-card-file.js b/ghost/admin/lib/koenig-editor/addon/components/koenig-card-file.js index d8f004704b..3f84b7fe5e 100644 --- a/ghost/admin/lib/koenig-editor/addon/components/koenig-card-file.js +++ b/ghost/admin/lib/koenig-editor/addon/components/koenig-card-file.js @@ -1,4 +1,5 @@ import Component from '@glimmer/component'; +import prettifyFileName from '../utils/prettify-file-name'; import {TrackedObject} from 'tracked-built-ins'; import {action} from '@ember/object'; import {bind} from '@ember/runloop'; @@ -6,17 +7,16 @@ import {isBlank} from '@ember/utils'; import {inject as service} from '@ember/service'; import {set} from '@ember/object'; import {tracked} from '@glimmer/tracking'; - const PLACEHOLDERS = ['summer', 'mountains', 'ufo-attack']; /* Payload { src: 'https://ghostsite.com/media/...', - fileSrc: '...', fileName: '...', fileSize: 2048, fileTitle: '...', fileCaption: '...', + mimeType: '...' } */ @@ -63,21 +63,9 @@ export default class KoenigCardFileComponent extends Component { }; } - bytesToSize(bytes) { - if (!bytes) { - return '0 Byte'; - } - const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; - if (bytes === 0) { - return '0 Byte'; - } - const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); - return Math.round((bytes / Math.pow(1024, i))) + ' ' + sizes[i]; - } - get fileSize() { const sizeInBytes = this.args.payload.fileSize || this.previewPayload.fileSize; - return this.bytesToSize(sizeInBytes); + return this._bytesToSize(sizeInBytes); } get fileName() { @@ -149,16 +137,11 @@ export default class KoenigCardFileComponent extends Component { } } - prettifyFileName(filename) { - let updatedName = filename.split('.').slice(0, -1).join('.').replace(/[-_]/g,' ').replace(/[^\w\s]+/g,'').replace(/\s\s+/g, ' '); - return updatedName.charAt(0).toUpperCase() + updatedName.slice(1); - } - @action async fileUploadCompleted([uploadedFile]) { this.previewPayload.src = uploadedFile.url; this.previewPayload.fileName = uploadedFile.fileName; - this.previewPayload.fileTitle = this.prettifyFileName(uploadedFile.fileName); + this.previewPayload.fileTitle = prettifyFileName(uploadedFile.fileName); this.previewPayload.fileCaption = ''; // save preview payload attrs into actual payload and create undo snapshot @@ -231,4 +214,16 @@ export default class KoenigCardFileComponent extends Component { this.files = [event.dataTransfer.files[0]]; } } + + _bytesToSize(bytes) { + if (!bytes) { + return '0 Byte'; + } + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; + if (bytes === 0) { + return '0 Byte'; + } + const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); + return Math.round((bytes / Math.pow(1024, i))) + ' ' + sizes[i]; + } }