UNPKG

trainingpeaks-sdk

Version:
63 lines (62 loc) 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateWorkoutFileName = exports.updateWorkoutFileContent = exports.getFileSize = exports.validateWorkoutFile = exports.createWorkoutFile = void 0; const domain_errors_1 = require("../../domain/errors/domain-errors.js"); const createWorkoutFile = (fileName, content, mimeType) => { const trimmedFileName = fileName.trim(); const trimmedMimeType = mimeType.trim(); if (!trimmedFileName) { throw new domain_errors_1.ValidationError('File name cannot be empty', 'fileName'); } if (trimmedFileName.length > 255) { throw new domain_errors_1.ValidationError('File name cannot exceed 255 characters', 'fileName'); } if (!trimmedMimeType) { throw new domain_errors_1.ValidationError('MIME type cannot be empty', 'mimeType'); } if (trimmedMimeType.length > 100) { throw new domain_errors_1.ValidationError('MIME type cannot exceed 100 characters', 'mimeType'); } if (!content) { throw new domain_errors_1.ValidationError('File content cannot be empty', 'content'); } const workoutFile = { fileName: trimmedFileName, content, mimeType: trimmedMimeType, }; return Object.freeze(workoutFile); }; exports.createWorkoutFile = createWorkoutFile; const validateWorkoutFile = (file) => { if (!file) { return false; } if (typeof file.fileName !== 'string' || typeof file.content !== 'string' || typeof file.mimeType !== 'string') { return false; } return (file.fileName.length > 0 && file.content.length > 0 && file.mimeType.length > 0); }; exports.validateWorkoutFile = validateWorkoutFile; const getFileSize = (file) => { if (typeof file.content !== 'string') { throw new domain_errors_1.ValidationError('File content must be a valid string to calculate size', 'content'); } return Buffer.byteLength(file.content, 'utf8'); }; exports.getFileSize = getFileSize; const updateWorkoutFileContent = (file, newContent) => { if (!newContent) { throw new domain_errors_1.ValidationError('File content cannot be empty', 'content'); } return (0, exports.createWorkoutFile)(file.fileName, newContent, file.mimeType); }; exports.updateWorkoutFileContent = updateWorkoutFileContent; const updateWorkoutFileName = (file, newFileName) => { return (0, exports.createWorkoutFile)(newFileName, file.content, file.mimeType); }; exports.updateWorkoutFileName = updateWorkoutFileName;