@trap_stevo/filetide
Version:
Revolutionizing real-time file transfer with seamless, instant communication across any device. Deliver files instantly, regardless of platform, and experience unparalleled speed and control in managing transfers. Elevate your file-sharing capabilities wi
93 lines (92 loc) • 3.29 kB
JavaScript
const path = require("path");
const fs = require("fs");
class FileTransferRecoveryManager {
constructor(storagePath = `${process.cwd()}/transfer_states`) {
if (FileTransferRecoveryManager.instance) {
return FileTransferRecoveryManager.instance;
}
this.storagePath = storagePath;
this.memoryStorage = new Map();
this.ensureStorageDirectory();
FileTransferRecoveryManager.instance = this;
}
ensureStorageDirectory() {
if (!fs.existsSync(this.storagePath)) {
fs.mkdirSync(this.storagePath, {
recursive: true
});
}
}
sanitizeFileName(fileName) {
return fileName.replace(/[^a-zA-Z0-9-_\.]/g, "-").replace(/-+/g, "-").trim();
}
generateKey(clientName, senderName, filePath) {
const normalizedPath = this.sanitizeFileName(filePath);
return `${clientName}_${senderName}_${normalizedPath}.json`;
}
getStorageFilePath(clientName, senderName, filePath) {
return path.join(this.storagePath, this.generateKey(clientName, senderName, filePath));
}
saveTransferState(clientName, senderName, filePath, chunkIndex, transferredSize) {
const key = this.generateKey(clientName, senderName, filePath);
const transferState = {
transferredSize,
senderName,
clientName,
filePath,
lastChunkSent: chunkIndex
};
this.memoryStorage.set(key, transferState);
}
saveToDisk(clientName, senderName, filePath) {
const key = this.generateKey(clientName, senderName, filePath);
const storageFile = this.getStorageFilePath(clientName, senderName, filePath);
const transferState = this.memoryStorage.get(key);
if (transferState) {
try {
fs.writeFileSync(storageFile, JSON.stringify(transferState, null, 2));
console.log(`Saved transfer state to disk ~ ${storageFile}`);
} catch (error) {
console.error(`Error saving transfer state for ${key} ~ ${error.message}`);
}
}
}
getTransferState(clientName, senderName, filePath) {
const key = this.generateKey(clientName, senderName, filePath);
if (this.memoryStorage.has(key)) {
return this.memoryStorage.get(key);
}
const storageFile = this.getStorageFilePath(clientName, senderName, filePath);
if (fs.existsSync(storageFile)) {
const transferState = JSON.parse(fs.readFileSync(storageFile, "utf-8"));
return transferState;
}
return null;
}
getLastChunk(clientName, senderName, filePath) {
const transferState = this.getTransferState(clientName, senderName, filePath);
return transferState ? transferState.lastChunkSent : null;
}
clearTransferState(clientName, senderName, filePath) {
const key = this.generateKey(clientName, senderName, filePath);
this.memoryStorage.delete(key);
const storageFile = this.getStorageFilePath(clientName, senderName, filePath);
if (fs.existsSync(storageFile)) {
fs.unlinkSync(storageFile);
}
}
flushToDisk() {
for (const [key, state] of this.memoryStorage.entries()) {
const {
clientName,
senderName,
filePath
} = state;
console.log("Saving current state ~ ", key);
this.saveToDisk(clientName, senderName, filePath);
}
;
}
}
module.exports = new FileTransferRecoveryManager();
;