@smash-sdk/downloader
Version:
<p align="center"> <a href="https://fromsmash.com/"><img src="https://developer.fromsmash.com/LOGO_SMASH_API.png" align="center" width="135" alt="Send big files"/></a> <h1 align="center">SmashDownloaderJS - Download library <br>powered by the Smash AP
112 lines • 5.79 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmashDownloader = void 0;
const core_1 = require("@smash-sdk/core");
const _10_2019_1 = require("@smash-sdk/link/10-2019");
const _07_2022_1 = require("@smash-sdk/transfer/07-2022");
const _10_2019_2 = require("@smash-sdk/transfer/10-2019");
const path_1 = __importDefault(require("path"));
const customEventEmitter_1 = require("./core/customEventEmitter");
const download_1 = require("./core/download");
const InvalidParameterError_1 = require("./errors/InvalidParameterError");
const constant_1 = require("./globals/constant");
const defaultConfig = {
enableOverride: false,
};
;
class SmashDownloader extends customEventEmitter_1.CustomEventEmitter {
constructor(config) {
super();
this.config = { ...defaultConfig, ...config };
if (!this.config.path && !this.config.stream) {
throw new InvalidParameterError_1.InvalidParameterError("You must specify path or stream");
}
if (this.config.stream && this.config.enableOverride === true) {
throw new InvalidParameterError_1.InvalidParameterError("You cannot specify enableOverride as true when passing stream parameter, this make no sense");
}
if (config.url && config.transferId) {
throw new InvalidParameterError_1.InvalidParameterError("You must specify url or transferId");
}
if (config.url && config.fileId) {
throw new InvalidParameterError_1.InvalidParameterError("You cannot specify fileId with url, feature is not supported");
}
}
download() {
return new Promise(async (resolve, reject) => {
try {
const meta = await this.getDownloadMetadata();
const downloadInstance = new download_1.Download({ downloadUrl: meta.downloadUrl, ...this.config });
downloadInstance.on(constant_1.DownloaderEvent.Progress, (event) => {
this.emit(constant_1.DownloaderEvent.Progress, event);
});
await downloadInstance.download();
const fileName = meta.fileName ? meta.fileName : downloadInstance.fileName;
const extension = meta.extension ? meta.extension : path_1.default.extname(fileName);
resolve({
transferId: meta.transferId,
name: meta.name,
fileId: meta.fileId,
availabilityEndDate: meta.availabilityEndDate,
region: meta.region,
availabilityDuration: meta.availabilityDuration,
filesNumber: meta.filesNumber,
created: meta.created,
availabilityStartDate: meta.availabilityStartDate,
transferUrl: meta.transferUrl,
size: downloadInstance.size,
path: this.config.path,
fileName,
extension,
});
}
catch (error) {
reject(error);
}
});
}
getDownloadMetadata() {
return new Promise(async (resolve, reject) => {
try {
if (this.config.url) {
const linkSDK = new _10_2019_1.Link({ token: this.config.token });
const url = new URL(this.config.url);
const { target } = await linkSDK.getTarget({ targetId: url.host + url.pathname });
this.config.transferId = target.target;
}
const { transferId, fileId, token } = this.config;
const region = (0, core_1.getRegionFromId)(this.config.transferId);
if (region && this.config.transferId) {
const transferSDK = new _10_2019_2.Transfer({ region, token });
const getTransferPreviewParams = { transferId: this.config.transferId };
if (this.config.password) {
getTransferPreviewParams["smash-authorization"] = encodeURIComponent(this.config.password);
}
const { transfer } = await transferSDK.getTransferPreview(getTransferPreviewParams);
if (this.config.fileId) {
const transferSDK = new _07_2022_1.Transfer({ region, token });
const getTransferFilePreviewParams = { transferId: this.config.transferId, fileId: this.config.fileId };
if (this.config.password) {
getTransferFilePreviewParams["smash-authorization"] = encodeURIComponent(this.config.password);
}
const { file } = await transferSDK.getTransferFilePreview(getTransferFilePreviewParams);
resolve({ ...transfer, downloadUrl: file.download, transferId: this.config.transferId, fileId: this.config.fileId, extension: file.ext, fileName: file.name });
}
else {
resolve({ ...transfer, downloadUrl: transfer.download, transferId: this.config.transferId, fileId: this.config.fileId, name: transfer.title });
}
}
else {
throw new InvalidParameterError_1.InvalidParameterError("Invalid transferId given: " + transferId);
}
}
catch (error) {
reject(error);
}
});
}
}
exports.SmashDownloader = SmashDownloader;
//# sourceMappingURL=SmashDownloader.js.map