@samluvanda/uploadx
Version:
Modern, lightweight, and extensible JavaScript file uploader supporting chunked uploads, filtering, retry logic, progress tracking, and event hooks.
65 lines (56 loc) • 1.75 kB
JavaScript
import { v4 as uuidv4 } from 'uuid';
const filePool = new Map();
export class File {
static STATUS = {
QUEUED: 'queued',
UPLOADING: 'uploading',
DONE: 'done',
FAILED: 'failed'
};
/**
* @param {File|Blob} file - Native file object from input or drag-drop
*/
constructor(file) {
this.id = uuidv4();
this.name = file.name || file.fileName;
this.type = file.type || '';
this.relativePath = file.relativePath || file.webkitRelativePath || '';
this.size = file.size || file.fileSize || 0;
this.origSize = this.size;
this.loaded = 0;
this.percent = 0;
this.status = File.STATUS.QUEUED;
this.lastModifiedDate =
file.lastModifiedDate?.toLocaleString?.() || new Date().toLocaleString();
this.completeTimestamp = 0;
this._native = file;
// Add to internal pool
filePool.set(this.id, file);
}
/**
* Returns the native File or Blob object.
* @returns {File|Blob|null}
*/
getNative() {
const src = this.getSource();
return src instanceof File || src instanceof Blob ? src : null;
}
/**
* Returns the file stored in the pool.
* @returns {File|Blob|null}
*/
getSource() {
return filePool.get(this.id) || null;
}
/**
* Cleans up references to this file.
*/
destroy() {
const src = this.getSource();
if (typeof src?.destroy === 'function') {
src.destroy(); // In case it's a wrapped class (like mOxie)
}
filePool.delete(this.id);
this._native = null;
}
}