@deepkit/framework
Version:
171 lines • 7.6 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaController = void 0;
function __assignType(fn, args) {
fn.__type = args;
return fn;
}
/*
* Deepkit Framework
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*
* You should have received a copy of the MIT License along with this program.
*/
const framework_debug_api_1 = require("@deepkit/framework-debug-api");
const rpc_1 = require("@deepkit/rpc");
const filesystem_js_1 = require("../filesystem.js");
const filesystem_1 = require("@deepkit/filesystem");
const core_1 = require("@deepkit/core");
const mime_types_1 = __importDefault(require("mime-types"));
function mapFile(filesystem, file) {
const mediaFile = new framework_debug_api_1.MediaFile(file.path, file.type);
mediaFile.size = file.size;
mediaFile.filesystem = filesystem;
mediaFile.lastModified = file.lastModified;
mediaFile.visibility = file.visibility;
mediaFile.mimeType = mime_types_1.default.lookup(file.path) || '';
return mediaFile;
}
mapFile.__type = ['filesystem', () => filesystem_1.FilesystemFile, 'file', () => framework_debug_api_1.MediaFile, 'mapFile', 'P\'2!P7"2#P7$/%'];
let MediaController = class MediaController {
constructor(filesystemRegistry) {
this.filesystemRegistry = filesystemRegistry;
}
getFilesystem(id) {
const fs = this.filesystemRegistry.getFilesystems()[id];
if (!fs)
throw new Error(`No filesystem with id ${id} found`);
return fs;
}
async getPublicUrl(fs, path) {
const filesystem = this.getFilesystem(fs);
return filesystem.publicUrl(path);
}
async createFolder(fs, path) {
const filesystem = this.getFilesystem(fs);
await filesystem.makeDirectory(path);
}
async getFile(fs, path) {
const filesystem = this.getFilesystem(fs);
const file = await filesystem.getOrUndefined(path);
if (!file)
return false;
return mapFile(fs, file);
}
async getFiles(fs, path) {
const filesystem = this.getFilesystem(fs);
const files = await filesystem.files(path);
return files.map(__assignType(file => mapFile(fs, file), ['file', '', 'P"2!"/"']));
}
async getMediaData(fs, path) {
const filesystem = this.getFilesystem(fs);
try {
return await filesystem.read(path);
}
catch (error) {
return false;
}
}
async getMediaQuickLook(fs, path) {
if (path === '/')
return false;
const filesystem = this.getFilesystem(fs);
const file = await filesystem.get(path);
const mimeType = mime_types_1.default.lookup(path) || '';
if (mimeType.startsWith('image/')) {
const data = await filesystem.read(path);
//todo: make smaller if too big
return { file: mapFile(fs, file), data: data };
}
return false;
}
async remove(fs, paths) {
const filesystem = this.getFilesystem(fs);
await filesystem.delete(paths);
}
async addFile(fs, name, dir, data) {
const filesystem = this.getFilesystem(fs);
await filesystem.write((0, core_1.pathJoin)(dir, name), data);
}
async renameFile(fs, path, newName) {
const filesystem = this.getFilesystem(fs);
filesystem.move(path, (0, core_1.pathJoin)((0, core_1.pathDirectory)(path), newName));
return Promise.resolve('');
}
};
exports.MediaController = MediaController;
MediaController.__type = [() => filesystem_js_1.FilesystemRegistry, 'filesystemRegistry', 'constructor', 'id', () => filesystem_1.Filesystem, 'getFilesystem', 'fs', 'path', 'getPublicUrl', 'createFolder', () => framework_debug_api_1.MediaFile, false, 'getFile', () => framework_debug_api_1.MediaFile, 'getFiles', false, 'getMediaData', () => framework_debug_api_1.MediaFile, 'file', 'data', false, 'getMediaQuickLook', 'paths', 'remove', 'name', 'dir', 'addFile', 'newName', 'renameFile', 'MediaController', 'PP7!2"<"0#P\'2$P7%0&<P\'2\'&2(&`0)P\'2\'&2($`0*P\'2\'&2(PP7+.,J`0-P\'2\'&2(P7.F`0/P\'2\'&2(PW.0J`01P\'2\'&2(PPP7243W44M.5J`06P\'2\'&F27$`08P\'2\'&29&2:W24$`0;P\'2\'&2(&2<&`0=5!x"w>'];
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "getPublicUrl", null);
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "createFolder", null);
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "getFile", null);
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "getFiles", null);
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "getMediaData", null);
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "getMediaQuickLook", null);
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, Array]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "remove", null);
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String, String, Uint8Array]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "addFile", null);
__decorate([
rpc_1.rpc.action(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Number, String, String]),
__metadata("design:returntype", Promise)
], MediaController.prototype, "renameFile", null);
exports.MediaController = MediaController = __decorate([
rpc_1.rpc.controller(framework_debug_api_1.DebugMediaInterface),
__metadata("design:paramtypes", [filesystem_js_1.FilesystemRegistry])
], MediaController);
//# sourceMappingURL=media.controller.js.map
;