@nu-art/file-upload
Version:
File Uploader - Express & Typescript based backend framework
81 lines (80 loc) • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModuleBE_AssetsStorage = exports.ModuleBE_AssetsStorage_Class = void 0;
/*
* 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.
*/
const backend_1 = require("@nu-art/thunderstorm/backend");
const ts_common_1 = require("@nu-art/ts-common");
const backend_2 = require("@nu-art/firebase/backend");
const shared_1 = require("../../shared");
const thunderstorm_1 = require("@nu-art/thunderstorm");
const ModuleBE_AssetsDB_1 = require("./ModuleBE_AssetsDB");
class ModuleBE_AssetsStorage_Class extends ts_common_1.Module {
constructor() {
super();
this.getReadSignedUrl = async (dbAsset) => (await (await this.getFile(dbAsset)).getReadSignedUrl()).signedUrl;
this.getFile = async (dbAsset) => this.storage.getFile(dbAsset.path, dbAsset.bucketName);
this.setDefaultConfig({ batchItemCount: 10 });
}
init() {
super.init();
// @ts-ignore
this.storage = backend_2.ModuleBE_Firebase.createAdminSession().getStorage();
}
async __onSyncEnvCompleted(env, baseUrl, requiredHeaders) {
this.logWarning('Not Implemented Yet', new ts_common_1.NotImplementedYetException('Sync assets not implemented'));
let assetsToSync = [];
let page = 0;
const itemsCount = this.config.batchItemCount;
let dbAssets;
do {
dbAssets = await ModuleBE_AssetsDB_1.ModuleBE_AssetsDB.query.custom({ limit: { page: page++, itemsCount } });
if (dbAssets.length === 0)
break;
assetsToSync = (0, ts_common_1.filterInstances)(await Promise.all(dbAssets.map(async (dbAsset) => {
const fileWrapper = await this.getFile(dbAsset);
if (await fileWrapper.exists())
return;
return dbAsset;
})));
if (assetsToSync.length > 0)
continue;
assetsToSync.map(async (dbAsset) => {
let _signedUrl;
try {
const { signedUrl } = await backend_1.AxiosHttpModule
.createRequest(Object.assign(Object.assign({}, shared_1.ApiDef_Assets.vv1.getReadSignedUrl), { baseUrl }))
.setHeaders(requiredHeaders)
.setBodyAsJson({ assetId: dbAsset._id })
.executeSync();
_signedUrl = signedUrl;
}
catch (e) {
console.log(e);
}
const fileContent = await backend_1.AxiosHttpModule.createRequest({ method: thunderstorm_1.HttpMethod.GET, fullUrl: _signedUrl, path: '' })
.setResponseType('text')
.executeSync();
await (await this.getFile(dbAsset)).write(fileContent);
});
} while (dbAssets.length > 0);
}
}
exports.ModuleBE_AssetsStorage_Class = ModuleBE_AssetsStorage_Class;
exports.ModuleBE_AssetsStorage = new ModuleBE_AssetsStorage_Class();