@nestjs-mod/files
Version:
Files module with an error filter, controller and rest-sdk for work with module from other nodejs appliaction
79 lines • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilesController = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs-mod/swagger");
const swagger_2 = require("@nestjs/swagger");
const nestjs_translates_1 = require("nestjs-translates");
const node_crypto_1 = require("node:crypto");
const files_configuration_1 = require("../files.configuration");
const files_decorators_1 = require("../files.decorators");
const files_environments_1 = require("../files.environments");
const files_errors_1 = require("../files.errors");
const dto_1 = require("../types/dto");
const files_role_1 = require("../types/files-role");
let FilesController = class FilesController {
constructor(filesConfiguration, filesStaticEnvironments) {
this.filesConfiguration = filesConfiguration;
this.filesStaticEnvironments = filesStaticEnvironments;
}
async getPresignedUrl(getPresignedUrlArgs, filesRequest, getText) {
const bucketName = Object.entries(this.filesConfiguration.buckets || {})
.filter(([, options]) => options.ext.includes(getPresignedUrlArgs.ext))
.map(([name]) => name)?.[0];
if (!bucketName) {
throw new files_errors_1.FilesError(getText('Uploading files with extension "{{ext}}" is not supported'), files_errors_1.FilesErrorEnum.FORBIDDEN, { ext: getPresignedUrlArgs.ext });
}
const fullObjectName = `${filesRequest.externalUserId ??
this.filesStaticEnvironments.filesDefaultUserId}/${bucketName}_${(0, node_crypto_1.randomUUID)()}.${getPresignedUrlArgs.ext}`;
return await this.filesConfiguration.getPresignedUrls({
bucketName,
fullObjectName,
ext: getPresignedUrlArgs.ext,
userId: filesRequest.externalUserId ??
this.filesStaticEnvironments.filesDefaultUserId,
});
}
async deleteFile(deleteFileArgs, filesRequest, getText) {
if (filesRequest.filesUser?.userRole === files_role_1.FilesRole.Admin ||
deleteFileArgs.downloadUrl.includes(`/${filesRequest.externalUserId}/`)) {
const { objectName, bucketName } = this.filesConfiguration.getFromDownloadUrlWithoutBucketNames(deleteFileArgs.downloadUrl);
await this.filesConfiguration.deleteFile({
bucketName,
objectName,
downloadUrl: deleteFileArgs.downloadUrl,
});
return { message: getText('ok') };
}
throw new files_errors_1.FilesError(getText('Only those who uploaded files can delete them'), files_errors_1.FilesErrorEnum.FORBIDDEN);
}
};
exports.FilesController = FilesController;
tslib_1.__decorate([
(0, common_1.Get)('/files/get-presigned-url'),
(0, swagger_2.ApiOkResponse)({ type: dto_1.FilesPresignedUrls }),
tslib_1.__param(0, (0, common_1.Query)()),
tslib_1.__param(1, (0, files_decorators_1.CurrentFilesRequest)()),
tslib_1.__param(2, (0, nestjs_translates_1.InjectTranslateFunction)()),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [dto_1.FilesGetPresignedUrlArgs, Object, Function]),
tslib_1.__metadata("design:returntype", Promise)
], FilesController.prototype, "getPresignedUrl", null);
tslib_1.__decorate([
(0, common_1.Post)('/files/delete-file'),
(0, common_1.HttpCode)(common_1.HttpStatus.OK),
(0, swagger_2.ApiOkResponse)({ type: swagger_1.StatusResponse }),
tslib_1.__param(0, (0, common_1.Query)()),
tslib_1.__param(1, (0, files_decorators_1.CurrentFilesRequest)()),
tslib_1.__param(2, (0, nestjs_translates_1.InjectTranslateFunction)()),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [dto_1.FilesDeleteFileArgs, Object, Function]),
tslib_1.__metadata("design:returntype", Promise)
], FilesController.prototype, "deleteFile", null);
exports.FilesController = FilesController = tslib_1.__decorate([
(0, common_1.Controller)(),
tslib_1.__metadata("design:paramtypes", [files_configuration_1.FilesConfiguration,
files_environments_1.FilesStaticEnvironments])
], FilesController);
//# sourceMappingURL=files.controller.js.map