@nu-art/file-upload
Version:
File Uploader - Express & Typescript based backend framework
63 lines • 2.83 kB
JavaScript
/*
* Permissions management system, define access level for each of
* your server apis, and restrict users by giving them access levels
*
* Copyright (C) 2020 Adam van der Kruk aka TacB0sS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { apiWithBody, apiWithQuery, ModuleFE_XHR, ThunderDispatcher } from '@nu-art/thunderstorm/frontend/index';
import { ApiDef_AssetUploader, FileStatus, PushKey_FileUploaded } from '../../shared/index.js';
import { ModuleBase_AssetUploader } from '../../shared/modules/ModuleBase_AssetUploader.js';
import { ModuleFE_PushPubSub } from '@nu-art/push-pub-sub/frontend/modules/ModuleFE_PushPubSub';
import { generateHex } from '@nu-art/ts-common';
export class ModuleFE_AssetUploader_Class extends ModuleBase_AssetUploader {
dispatch_fileStatusChange = new ThunderDispatcher('__onFileStatusChanged');
vv1;
constructor() {
super();
this.vv1 = {
getUploadUrl: apiWithBody(ApiDef_AssetUploader.vv1.getUploadUrl),
processAssetManually: apiWithQuery(ApiDef_AssetUploader.vv1.processAssetManually),
};
}
upload(files, key, _public = false) {
return this.uploadImpl(files.map((file => {
return {
feId: generateHex(32),
name: file.name,
mimeType: file.type,
key,
file,
ext: ''
};
})));
}
createRequest(uploadFile) {
return ModuleFE_XHR.createRequest(uploadFile);
}
dispatchFileStatusChange(id) {
this.dispatch_fileStatusChange.dispatchUI(id);
}
async subscribeToPush(toSubscribe) {
const subscriptions = toSubscribe.map(r => ({ topic: PushKey_FileUploaded, props: { feId: r.asset.feId } }));
await ModuleFE_PushPubSub.v1.registerAll(subscriptions).executeSync();
}
__onMessageReceived(notification) {
super.__onMessageReceived(notification);
if (notification.message?.status === FileStatus.Completed || notification.message?.status?.startsWith('Error'))
ModuleFE_PushPubSub.v1.unregister({ topic: PushKey_FileUploaded, filter: notification.filter });
}
}
export const ModuleFE_AssetUploader = new ModuleFE_AssetUploader_Class();
//# sourceMappingURL=ModuleFE_AssetUploader.js.map