UNPKG

rcu-fastify

Version:
135 lines (131 loc) 4.82 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { JsonStoreProvider: () => import_rcu_back_core2.JsonStoreProvider, RCUConfig: () => import_rcu_back_core2.RCUConfig, StoreProviderInterface: () => import_rcu_back_core2.StoreProviderInterface, Upload: () => import_rcu_back_core2.Upload, resumableChunkUpload: () => resumableChunkUpload }); module.exports = __toCommonJS(src_exports); var import_rcu_back_core = require("rcu-back-core"); // src/handler.ts var import_zod = require("zod"); var import_multipart = __toESM(require("@fastify/multipart")); var uploadStatusSchema = import_zod.z.object({ fileId: import_zod.z.string().min(1), chunkCount: import_zod.z.coerce.number() }); var uploadSchema = import_zod.z.object({ fileId: import_zod.z.string().min(1), chunkNumber: import_zod.z.coerce.number(), originalFilename: import_zod.z.string().min(1), chunkCount: import_zod.z.coerce.number(), chunkSize: import_zod.z.coerce.number(), fileSize: import_zod.z.coerce.number(), file: import_zod.z.instanceof(Buffer) }); function handler(service, config) { const { uploadStatusPath = "/uploadStatus", uploadPath = "/upload" } = config ?? {}; return async (fastify) => { fastify.register(import_multipart.default, { attachFieldsToBody: true }); fastify.get( uploadStatusPath, async (request, reply) => { try { const query = uploadStatusSchema.parse(request.query); const response = await service.uploadStatus(query); return reply.send(response); } catch (error) { if (error instanceof import_zod.ZodError) { return reply.status(400).send({ message: "Invalid parameter" }); } return reply.status(500).send({ message: "Internal server error" }); } } ); fastify.post( uploadPath, async (request, reply) => { try { const originalBody = request.body ?? {}; const body = Object.fromEntries( Object.keys(originalBody).map((key) => [ key, originalBody[key].value ]) ); const dto = await uploadSchema.parse({ ...body, file: await originalBody.file?.toBuffer() }); const response = await service.upload(dto); return reply.send(response); } catch (error) { if (error.code === "FST_INVALID_MULTIPART_CONTENT_TYPE") { return reply.status(400).send({ message: "Invalid parameter" }); } if (error instanceof import_zod.ZodError) { return reply.status(400).send({ message: "Invalid parameter" }); } return reply.status(500).send({ message: "Internal server error" }); } } ); }; } // src/index.ts var import_rcu_back_core2 = require("rcu-back-core"); function resumableChunkUpload(config = {}) { const { store = new import_rcu_back_core.JsonStoreProvider("./tmp/rcu.json"), tmpDir = "./tmp", outputDir, onCompleted } = config; return handler( new import_rcu_back_core.RCUService({ store, tmpDir, outputDir: outputDir ?? tmpDir, onCompleted }), config ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { JsonStoreProvider, RCUConfig, StoreProviderInterface, Upload, resumableChunkUpload });