media-exporter-processor
Version:
Media processing API with thumbnail generation and cloud storage
117 lines • 3.71 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileUtils = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
class FileUtils {
/**
* Generate a unique temporary file path
*/
static generateTempPath(extension = "") {
const timestamp = Date.now();
const random = Math.random().toString(36).substring(2, 8);
const filename = `temp-${timestamp}-${random}${extension}`;
return path.join("/tmp", filename);
}
/**
* Write buffer to temporary file and return the path
*/
static async writeToTempFile(buffer, extension = ".mp4") {
const tempPath = this.generateTempPath(extension);
await fs_1.promises.writeFile(tempPath, buffer);
return tempPath;
}
/**
* Clean up multiple files, ignoring errors
*/
static async cleanupFiles(filePaths) {
await Promise.allSettled(filePaths.map(async (filePath) => {
try {
await fs_1.promises.unlink(filePath);
}
catch (error) {
// Ignore cleanup errors - files might not exist
}
}));
}
/**
* Check if file exists
*/
static async fileExists(filePath) {
try {
await fs_1.promises.access(filePath);
return true;
}
catch {
return false;
}
}
/**
* Get file size in bytes
*/
static async getFileSize(filePath) {
const stats = await fs_1.promises.stat(filePath);
return stats.size;
}
/**
* Extract filename from path without extension
*/
static getBasename(filePath) {
return path.basename(filePath, path.extname(filePath));
}
/**
* Get file extension from path
*/
static getExtension(filePath) {
return path.extname(filePath);
}
/**
* Ensure directory exists
*/
static async ensureDir(dirPath) {
try {
await fs_1.promises.mkdir(dirPath, { recursive: true });
}
catch (error) {
// Ignore error if directory already exists
if (error.code !== "EEXIST") {
throw error;
}
}
}
}
exports.FileUtils = FileUtils;
//# sourceMappingURL=FileUtils.js.map