Skip to content

Commit 8997cb2

Browse files
committed
Extend base storage, resolve deps with IoC container ♻️
1 parent d1ede50 commit 8997cb2

File tree

32 files changed

+289
-124
lines changed

32 files changed

+289
-124
lines changed

client/api/asset.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import request from './request';
22

33
const urls = {
4-
base: 'assets'
4+
base: 'assets',
5+
repository: repositoryId => `repositories/${repositoryId}/assets`
56
};
67

7-
function getUrl(folder, key) {
8-
const params = { key: folder ? `${folder}/${key}` : key };
8+
function getUrl(key) {
9+
const params = { key };
910
return request.get(urls.base, { params }).then(res => res.data.url);
1011
}
1112

1213
function upload(data) {
1314
return request.post(urls.base, data).then(res => res.data);
1415
}
1516

17+
function getRepositoryAssetUrl(repositoryId, key) {
18+
const params = { key };
19+
return request.get(urls.repository(repositoryId), { params })
20+
.then(res => res.data.url);
21+
}
22+
23+
function uploadRepositoryAsset(repositoryId, data) {
24+
return request.post(urls.repository(repositoryId), data)
25+
.then(res => res.data);
26+
}
27+
1628
export default {
1729
getUrl,
18-
upload
30+
upload,
31+
getRepositoryAssetUrl,
32+
uploadRepositoryAsset
1933
};

client/components/common/FileInput.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@upload="$emit('upload', $event)"
55
@uploading="$emit('update:uploading', $event)"
66
@delete="$emit('delete', $event)"
7-
:folder="folder">
7+
:repository-id="repositoryId">
88
<form @submit.prevent>
99
<v-file-input
1010
v-if="!fileKey"
@@ -50,7 +50,7 @@ export default {
5050
id: { type: String, default: () => uniqueId('file_') },
5151
fileKey: { type: String, default: '' },
5252
fileName: { type: String, default: '' },
53-
folder: { type: String, default: null },
53+
repositoryId: { type: Number, default: null },
5454
validate: { type: Object, default: () => ({ ext: [] }) },
5555
label: { type: String, default: 'File upload' },
5656
placeholder: { type: String, default: 'Choose a file' },

client/components/common/InputAsset.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
:uploading.sync="uploading"
1616
:validate="{ ext: extensions }"
1717
:confirm-deletion="false"
18-
:folder="folder"
18+
:repository-id="repositoryId"
1919
:label="uploadLabel"
2020
class="upload-btn" />
2121
<template v-if="file">
@@ -94,7 +94,7 @@ function isUploaded(url) {
9494
export default {
9595
name: 'input-asset',
9696
props: {
97-
folder: { type: String, default: null },
97+
repositoryId: { type: Number, default: null },
9898
url: { type: String, default: null },
9999
publicUrl: { type: String, default: null },
100100
extensions: { type: Array, required: true },

client/components/common/UploadProvider.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default {
1313
inject: ['$storageService'],
1414
mixins: [downloadMixin],
1515
props: {
16-
folder: { type: String, default: null }
16+
repositoryId: { type: Number, default: null }
1717
},
1818
data: () => ({ uploading: false }),
1919
methods: {
@@ -23,11 +23,14 @@ export default {
2323
const [file] = e.target.files;
2424
if (!file) return;
2525
this.form.append('file', file, file.name);
26-
if (this.folder) this.form.append('folder', this.folder);
26+
},
27+
upload(data) {
28+
if (!this.repositoryId) return this.storageService.upload(data);
29+
return this.$storageService.uploadRepositoryAsset(this.repositoryId, data);
2730
},
2831
uploadFile: loader(function (e) {
2932
this.createFileForm(e);
30-
return this.$storageService.upload(this.form)
33+
return this.upload(this.form)
3134
.then(data => {
3235
const { name } = this.form.get('file');
3336
this.$emit('upload', { ...data, name });
@@ -36,7 +39,9 @@ export default {
3639
});
3740
}, 'uploading'),
3841
async downloadFile(key, name) {
39-
const url = await this.$storageService.getUrl(this.repositoryId, key);
42+
const url = this.repositoryId
43+
? await this.$storageService.getRepositoryAssetUrl(this.repositoryId, key)
44+
: await this.$storageService.getUrl(key);
4045
return this.download(url, name);
4146
},
4247
deleteFile(item) {

client/components/common/tce-core/UploadBtn.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@upload="$emit('upload', $event)"
66
@uploading="$emit('update:uploading', $event)"
77
@delete="$emit('delete', $event)"
8-
:folder="folder"
8+
:repository-id="repositoryId"
99
class="file-upload">
1010
<form @submit.prevent class="upload-form">
1111
<validation-provider ref="validator" :rules="validate">
@@ -52,7 +52,7 @@ export default {
5252
id: { type: String, default: () => uniqueId('file_') },
5353
fileName: { type: String, default: '' },
5454
fileKey: { type: String, default: '' },
55-
folder: { type: String, default: null },
55+
repositoryId: { type: Number, default: null },
5656
validate: { type: Object, default: () => ({ ext: [] }) },
5757
label: { type: String, default: 'Choose a file' },
5858
sm: { type: Boolean, default: false }

client/components/content-elements/tce-audio/edit/Toolbar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<v-toolbar-title class="pl-1">Audio Component</v-toolbar-title>
77
<input-asset
88
@input="save"
9-
:folder="`repository/${element.repositoryId}`"
9+
:repository-id="element.repositoryId"
1010
:url="url"
1111
:public-url="publicUrl"
1212
:extensions="[

client/components/content-elements/tce-image/edit/Toolbar.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<v-toolbar-items class="mx-auto">
88
<upload-btn
99
@change="upload"
10-
:folder="`repository/${element.repositoryId}`"
1110
:label="isUploaded ? 'Upload a new image' : 'Upload image'" />
1211
<template v-if="isUploaded">
1312
<v-btn @click="toggleTool('cropper')" text>

client/components/content-elements/tce-image/server/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const mime = require('mime-types');
88

99
const DEFAULT_IMAGE_EXTENSION = 'png';
1010

11-
function processImage(asset, { storage }) {
11+
function processImage(asset, { getRepositoryStorage }) {
1212
const image = asset.data.url;
1313
const base64Pattern = /^data:image\/(\w+);base64,/;
1414

@@ -22,21 +22,24 @@ function processImage(asset, { storage }) {
2222
return Promise.resolve(asset);
2323
}
2424

25+
const storage = getRepositoryStorage(asset.repositoryId);
2526
const file = Buffer.from(image.replace(base64Pattern, ''), 'base64');
2627
const extension = image.match(base64Pattern)[1] || DEFAULT_IMAGE_EXTENSION;
2728
const hashString = `${asset.id}${file}`;
2829
const hash = crypto.createHash('md5').update(hashString).digest('hex');
29-
const storagePath = storage.getPath(asset.repositoryId);
30-
const key = `${storagePath}/${asset.id}/${hash}.${extension}`;
30+
const key = `${asset.id}/${hash}.${extension}`;
3131
asset.data.url = key;
3232
return saveFile(key, file, storage).then(() => asset);
3333
}
3434

35-
function resolveImage(asset, { storage, storageProxy }) {
35+
function resolveImage(asset, { getRepositoryStorage, storageProxy }) {
3636
if (!asset.data || !asset.data.url) return Promise.resolve(asset);
3737

38+
const storage = getRepositoryStorage(asset.repositoryId);
39+
3840
function getUrl(key) {
39-
asset.data.url = storageProxy.getFileUrl(key);
41+
const fullKey = storage.getFullKey(key);
42+
asset.data.url = storageProxy.getFileUrl(fullKey);
4043
return asset;
4144
}
4245

client/components/content-elements/tce-pdf/edit/Toolbar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<v-toolbar-title>PDF Component</v-toolbar-title>
77
<input-asset
88
@input="save"
9-
:folder="`repository/${element.repositoryId}`"
9+
:repository-id="element.repositoryId"
1010
:url="url"
1111
:public-url="publicUrl"
1212
:extensions="['.pdf']"

client/components/content-elements/tce-video/edit/Toolbar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<v-toolbar-title class="pl-1">Video component</v-toolbar-title>
77
<input-asset
88
@input="save"
9-
:folder="`repository/${element.repositoryId}`"
9+
:repository-id="element.repositoryId"
1010
:url="url"
1111
:public-url="publicUrl"
1212
:extensions="['.mp4']"

0 commit comments

Comments
 (0)