@scalemule/sdk 0.0.55 → 0.0.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +135 -1
- package/dist/index.d.ts +135 -1
- package/dist/index.js +602 -0
- package/dist/index.mjs +600 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -6190,6 +6190,15 @@ var PhotoService = class extends ServiceModule {
|
|
|
6190
6190
|
async delete(id, options) {
|
|
6191
6191
|
return this.del(`/${id}`, options);
|
|
6192
6192
|
}
|
|
6193
|
+
async getPresets(options) {
|
|
6194
|
+
return this._get("/presets", options);
|
|
6195
|
+
}
|
|
6196
|
+
async getManifest(id, params, options) {
|
|
6197
|
+
return this._get(this.withQuery(`/${id}/manifest`, params), options);
|
|
6198
|
+
}
|
|
6199
|
+
async getPublicManifest(id, params, options) {
|
|
6200
|
+
return this._get(this.withQuery(`/public/${id}/manifest`, params), options);
|
|
6201
|
+
}
|
|
6193
6202
|
/**
|
|
6194
6203
|
* Build an absolute URL for the on-demand transform endpoint.
|
|
6195
6204
|
*
|
|
@@ -6439,6 +6448,594 @@ var PhotoService = class extends ServiceModule {
|
|
|
6439
6448
|
}
|
|
6440
6449
|
};
|
|
6441
6450
|
|
|
6451
|
+
// src/services/media.ts
|
|
6452
|
+
var MEDIA_PRESETS = {
|
|
6453
|
+
hero: { widths: [400, 800, 1200, 1600, 2400] },
|
|
6454
|
+
inline: { widths: [400, 800, 1200] },
|
|
6455
|
+
thumbnail: { widths: [120, 240, 360], fit: "cover" },
|
|
6456
|
+
avatar: { widths: [64, 128, 256], fit: "cover" },
|
|
6457
|
+
logo: { widths: [120, 240, 480] }
|
|
6458
|
+
};
|
|
6459
|
+
var MediaService = class extends ServiceModule {
|
|
6460
|
+
constructor(client, storage, photo, video, audio) {
|
|
6461
|
+
super(client);
|
|
6462
|
+
this.storage = storage;
|
|
6463
|
+
this.photo = photo;
|
|
6464
|
+
this.video = video;
|
|
6465
|
+
this.audio = audio;
|
|
6466
|
+
this.basePath = "";
|
|
6467
|
+
}
|
|
6468
|
+
getPresets() {
|
|
6469
|
+
return MEDIA_PRESETS;
|
|
6470
|
+
}
|
|
6471
|
+
async upload(file, options, requestOptions) {
|
|
6472
|
+
const mimeType = file.type || "application/octet-stream";
|
|
6473
|
+
const kind = detectKind(mimeType);
|
|
6474
|
+
const visibility = resolveVisibility(options);
|
|
6475
|
+
const isPublic = visibility !== "private";
|
|
6476
|
+
const isAnonymous = visibility === "anonymous_visible";
|
|
6477
|
+
const gateOnPipeline = shouldGateOnPipeline(options?.policy);
|
|
6478
|
+
const emit = (phase, progress, fileId) => {
|
|
6479
|
+
options?.onProgress?.({ phase, progress, file_id: fileId });
|
|
6480
|
+
};
|
|
6481
|
+
const uploadOptions = {
|
|
6482
|
+
filename: options?.filename,
|
|
6483
|
+
metadata: options?.metadata,
|
|
6484
|
+
signal: options?.signal,
|
|
6485
|
+
onProgress: (progress) => emit("uploading", progress)
|
|
6486
|
+
};
|
|
6487
|
+
emit("presigning", 0);
|
|
6488
|
+
try {
|
|
6489
|
+
if (isAnonymous) {
|
|
6490
|
+
return await this.uploadAnonymous(file, mimeType, kind, options, requestOptions, uploadOptions, emit);
|
|
6491
|
+
}
|
|
6492
|
+
if (kind === "image" && !options?.skipPhotoRegister) {
|
|
6493
|
+
return await this.uploadImage(
|
|
6494
|
+
file,
|
|
6495
|
+
mimeType,
|
|
6496
|
+
visibility,
|
|
6497
|
+
gateOnPipeline,
|
|
6498
|
+
options,
|
|
6499
|
+
requestOptions,
|
|
6500
|
+
uploadOptions,
|
|
6501
|
+
emit
|
|
6502
|
+
);
|
|
6503
|
+
}
|
|
6504
|
+
if (kind === "video" && visibility === "private") {
|
|
6505
|
+
const result = await this.video.uploadViaStorage(file, uploadOptions, requestOptions);
|
|
6506
|
+
if (result.error || !result.data) {
|
|
6507
|
+
return { data: null, error: result.error };
|
|
6508
|
+
}
|
|
6509
|
+
if (gateOnPipeline) {
|
|
6510
|
+
emit("optimizing", 100, result.data.file_id);
|
|
6511
|
+
await result.data.hls_url_promise;
|
|
6512
|
+
}
|
|
6513
|
+
const info = await this.fetchInfoBestEffort(result.data.file_id, requestOptions);
|
|
6514
|
+
const asset2 = this.buildAsset(
|
|
6515
|
+
info ?? fallbackFileInfo(result.data.file_id, mimeType, visibility, result.data.original_view_url, null),
|
|
6516
|
+
{
|
|
6517
|
+
photoId: null,
|
|
6518
|
+
preset: options?.preset,
|
|
6519
|
+
customWidths: options?.customWidths
|
|
6520
|
+
}
|
|
6521
|
+
);
|
|
6522
|
+
emit("ready", 100, result.data.file_id);
|
|
6523
|
+
return {
|
|
6524
|
+
data: {
|
|
6525
|
+
file_id: result.data.file_id,
|
|
6526
|
+
photo_id: null,
|
|
6527
|
+
original_view_url: result.data.original_view_url,
|
|
6528
|
+
optimized_url_promise: Promise.resolve(null),
|
|
6529
|
+
hls_url_promise: result.data.hls_url_promise,
|
|
6530
|
+
mime_type: mimeType,
|
|
6531
|
+
is_public: false,
|
|
6532
|
+
visibility,
|
|
6533
|
+
cdn_url: null,
|
|
6534
|
+
asset: asset2
|
|
6535
|
+
},
|
|
6536
|
+
error: null
|
|
6537
|
+
};
|
|
6538
|
+
}
|
|
6539
|
+
if (kind === "audio" && visibility === "private") {
|
|
6540
|
+
const result = await this.audio.uploadViaStorage(file, uploadOptions, requestOptions);
|
|
6541
|
+
if (result.error || !result.data) {
|
|
6542
|
+
return { data: null, error: result.error };
|
|
6543
|
+
}
|
|
6544
|
+
const info = await this.fetchInfoBestEffort(result.data.file_id, requestOptions);
|
|
6545
|
+
const asset2 = this.buildAsset(
|
|
6546
|
+
info ?? fallbackFileInfo(result.data.file_id, mimeType, visibility, result.data.original_view_url, null),
|
|
6547
|
+
{
|
|
6548
|
+
photoId: null,
|
|
6549
|
+
preset: options?.preset,
|
|
6550
|
+
customWidths: options?.customWidths
|
|
6551
|
+
}
|
|
6552
|
+
);
|
|
6553
|
+
emit("ready", 100, result.data.file_id);
|
|
6554
|
+
return {
|
|
6555
|
+
data: {
|
|
6556
|
+
file_id: result.data.file_id,
|
|
6557
|
+
photo_id: null,
|
|
6558
|
+
original_view_url: result.data.original_view_url,
|
|
6559
|
+
optimized_url_promise: Promise.resolve(null),
|
|
6560
|
+
hls_url_promise: Promise.resolve(null),
|
|
6561
|
+
mime_type: mimeType,
|
|
6562
|
+
is_public: false,
|
|
6563
|
+
visibility,
|
|
6564
|
+
cdn_url: null,
|
|
6565
|
+
asset: asset2
|
|
6566
|
+
},
|
|
6567
|
+
error: null
|
|
6568
|
+
};
|
|
6569
|
+
}
|
|
6570
|
+
const storageResult = isPublic ? await this.storage.upload(file, {
|
|
6571
|
+
...uploadOptions,
|
|
6572
|
+
visibility: "app_public",
|
|
6573
|
+
skipCompression: true
|
|
6574
|
+
}) : await this.storage.uploadPrivate(file, uploadOptions);
|
|
6575
|
+
if (storageResult.error || !storageResult.data) {
|
|
6576
|
+
return { data: null, error: storageResult.error };
|
|
6577
|
+
}
|
|
6578
|
+
const fileInfo = storageResult.data;
|
|
6579
|
+
const asset = this.buildAsset(fileInfo, {
|
|
6580
|
+
photoId: null,
|
|
6581
|
+
preset: options?.preset,
|
|
6582
|
+
customWidths: options?.customWidths
|
|
6583
|
+
});
|
|
6584
|
+
emit("ready", 100, fileInfo.id);
|
|
6585
|
+
return {
|
|
6586
|
+
data: {
|
|
6587
|
+
file_id: fileInfo.id,
|
|
6588
|
+
photo_id: null,
|
|
6589
|
+
original_view_url: fileInfo.url ?? null,
|
|
6590
|
+
optimized_url_promise: Promise.resolve(null),
|
|
6591
|
+
hls_url_promise: Promise.resolve(null),
|
|
6592
|
+
mime_type: mimeType,
|
|
6593
|
+
is_public: fileInfo.is_public ?? isPublic,
|
|
6594
|
+
visibility: resolveFileVisibility(fileInfo, visibility),
|
|
6595
|
+
cdn_url: fileInfo.cdn_url ?? null,
|
|
6596
|
+
asset
|
|
6597
|
+
},
|
|
6598
|
+
error: null
|
|
6599
|
+
};
|
|
6600
|
+
} catch (error) {
|
|
6601
|
+
return {
|
|
6602
|
+
data: null,
|
|
6603
|
+
error: normalizeThrownError(error)
|
|
6604
|
+
};
|
|
6605
|
+
}
|
|
6606
|
+
}
|
|
6607
|
+
async get(fileId, options, requestOptions) {
|
|
6608
|
+
const result = await this.storage.getInfo(fileId, requestOptions);
|
|
6609
|
+
if (result.error || !result.data) {
|
|
6610
|
+
return { data: null, error: result.error };
|
|
6611
|
+
}
|
|
6612
|
+
return {
|
|
6613
|
+
data: this.buildAsset(result.data, {
|
|
6614
|
+
photoId: null,
|
|
6615
|
+
preset: options?.preset,
|
|
6616
|
+
customWidths: options?.customWidths
|
|
6617
|
+
}),
|
|
6618
|
+
error: null
|
|
6619
|
+
};
|
|
6620
|
+
}
|
|
6621
|
+
async list(params, requestOptions) {
|
|
6622
|
+
const result = await this.storage.list(params, requestOptions);
|
|
6623
|
+
return {
|
|
6624
|
+
data: result.data.map(
|
|
6625
|
+
(file) => this.buildAsset(file, {
|
|
6626
|
+
photoId: null,
|
|
6627
|
+
preset: params?.preset,
|
|
6628
|
+
customWidths: params?.customWidths
|
|
6629
|
+
})
|
|
6630
|
+
),
|
|
6631
|
+
metadata: result.metadata,
|
|
6632
|
+
error: result.error
|
|
6633
|
+
};
|
|
6634
|
+
}
|
|
6635
|
+
async delete(fileId, options) {
|
|
6636
|
+
try {
|
|
6637
|
+
const [, storageResult] = await Promise.all([
|
|
6638
|
+
this.photo.delete(fileId, options).catch(() => ({ data: null, error: null })),
|
|
6639
|
+
this.storage.delete(fileId, options)
|
|
6640
|
+
]);
|
|
6641
|
+
if (storageResult.error && storageResult.error.status !== 404) {
|
|
6642
|
+
return { data: null, error: storageResult.error };
|
|
6643
|
+
}
|
|
6644
|
+
return { data: { deleted: true }, error: null };
|
|
6645
|
+
} catch (error) {
|
|
6646
|
+
return { data: null, error: normalizeThrownError(error) };
|
|
6647
|
+
}
|
|
6648
|
+
}
|
|
6649
|
+
async releaseQuarantine(fileId, reason = "Released via @scalemule/sdk media.releaseQuarantine()", options) {
|
|
6650
|
+
const response = await this.client.post(
|
|
6651
|
+
`/v1/storage/admin/files/${fileId}/scan-override`,
|
|
6652
|
+
{ scan_status: "clean", reason },
|
|
6653
|
+
options
|
|
6654
|
+
);
|
|
6655
|
+
if (response.error) {
|
|
6656
|
+
return { data: null, error: response.error };
|
|
6657
|
+
}
|
|
6658
|
+
return this.get(fileId, void 0, options);
|
|
6659
|
+
}
|
|
6660
|
+
async getManifest(fileId, options, requestOptions) {
|
|
6661
|
+
const file = await this.storage.getInfo(fileId, requestOptions);
|
|
6662
|
+
if (file.error || !file.data) {
|
|
6663
|
+
return { data: null, error: file.error };
|
|
6664
|
+
}
|
|
6665
|
+
const manifest = await this.fetchManifest(file.data, options, requestOptions);
|
|
6666
|
+
return { data: manifest, error: null };
|
|
6667
|
+
}
|
|
6668
|
+
buildAsset(file, options, manifest) {
|
|
6669
|
+
const visibility = resolveFileVisibility(file);
|
|
6670
|
+
const photoId = options?.photoId ?? null;
|
|
6671
|
+
const originalUrl = visibility === "anonymous_visible" ? file.cdn_url ?? file.url ?? null : file.url ?? file.cdn_url ?? null;
|
|
6672
|
+
return {
|
|
6673
|
+
file_id: file.id,
|
|
6674
|
+
photo_id: photoId,
|
|
6675
|
+
filename: file.filename,
|
|
6676
|
+
content_type: file.content_type,
|
|
6677
|
+
size_bytes: file.size_bytes,
|
|
6678
|
+
kind: detectKind(file.content_type),
|
|
6679
|
+
visibility,
|
|
6680
|
+
is_public: file.is_public ?? visibility !== "private",
|
|
6681
|
+
scan_status: file.scan_status,
|
|
6682
|
+
created_at: file.created_at,
|
|
6683
|
+
original_url: originalUrl,
|
|
6684
|
+
cdn_url: file.cdn_url ?? null,
|
|
6685
|
+
manifest: manifest ?? this.buildManifest(file, options)
|
|
6686
|
+
};
|
|
6687
|
+
}
|
|
6688
|
+
buildManifest(file, options) {
|
|
6689
|
+
const kind = detectKind(file.content_type);
|
|
6690
|
+
const visibility = resolveFileVisibility(file);
|
|
6691
|
+
const originalUrl = visibility === "anonymous_visible" ? file.cdn_url ?? file.url ?? null : file.url ?? file.cdn_url ?? null;
|
|
6692
|
+
if (!originalUrl) {
|
|
6693
|
+
return null;
|
|
6694
|
+
}
|
|
6695
|
+
if (kind !== "image" || file.content_type === "image/svg+xml") {
|
|
6696
|
+
return {
|
|
6697
|
+
file_id: file.id,
|
|
6698
|
+
content_type: file.content_type,
|
|
6699
|
+
preset: "original",
|
|
6700
|
+
variants: { original: originalUrl },
|
|
6701
|
+
srcset: null,
|
|
6702
|
+
default: originalUrl
|
|
6703
|
+
};
|
|
6704
|
+
}
|
|
6705
|
+
const preset = options?.preset ?? "inline";
|
|
6706
|
+
const spec = resolvePresetSpec(preset, options?.customWidths);
|
|
6707
|
+
const variants = Object.fromEntries(
|
|
6708
|
+
spec.widths.map((width) => [
|
|
6709
|
+
String(width),
|
|
6710
|
+
visibility === "anonymous_visible" ? this.photo.getPublicTransformUrl(file.id, transformOptions(width, spec.fit)) : this.photo.getTransformUrl(file.id, transformOptions(width, spec.fit))
|
|
6711
|
+
])
|
|
6712
|
+
);
|
|
6713
|
+
const defaultWidth = pickDefaultWidth(spec.widths);
|
|
6714
|
+
const defaultUrl = variants[String(defaultWidth)] ?? Object.values(variants)[0] ?? originalUrl;
|
|
6715
|
+
return {
|
|
6716
|
+
file_id: file.id,
|
|
6717
|
+
content_type: file.content_type,
|
|
6718
|
+
preset,
|
|
6719
|
+
variants,
|
|
6720
|
+
srcset: spec.widths.map((width) => `${variants[String(width)]} ${width}w`).join(", "),
|
|
6721
|
+
default: defaultUrl
|
|
6722
|
+
};
|
|
6723
|
+
}
|
|
6724
|
+
async uploadAnonymous(file, mimeType, kind, options, requestOptions, uploadOptions, emit) {
|
|
6725
|
+
const result = await this.storage.uploadAnonymous(file, uploadOptions);
|
|
6726
|
+
if (result.error || !result.data) {
|
|
6727
|
+
return { data: null, error: result.error };
|
|
6728
|
+
}
|
|
6729
|
+
emit("scanning", 100, result.data.id);
|
|
6730
|
+
const ready = await this.waitForAnonymousReady(result.data.id, options?.signal, emit);
|
|
6731
|
+
if (ready.error) {
|
|
6732
|
+
return { data: null, error: ready.error };
|
|
6733
|
+
}
|
|
6734
|
+
const fileInfo = await this.fetchInfoBestEffort(result.data.id, requestOptions) ?? {
|
|
6735
|
+
...result.data,
|
|
6736
|
+
cdn_url: ready.cdnUrl ?? result.data.cdn_url,
|
|
6737
|
+
scan_status: ready.scanStatus
|
|
6738
|
+
};
|
|
6739
|
+
let manifest = this.buildManifest(fileInfo, {
|
|
6740
|
+
preset: options?.preset,
|
|
6741
|
+
customWidths: options?.customWidths
|
|
6742
|
+
});
|
|
6743
|
+
if (kind === "image" && fileInfo.content_type !== "image/svg+xml" && options?.preset !== "custom") {
|
|
6744
|
+
emit("optimizing", 100, result.data.id);
|
|
6745
|
+
const persistedManifest = await this.waitForAnonymousManifest(fileInfo.id, options, requestOptions, emit);
|
|
6746
|
+
if (persistedManifest.error) {
|
|
6747
|
+
return { data: null, error: persistedManifest.error };
|
|
6748
|
+
}
|
|
6749
|
+
manifest = persistedManifest.manifest ?? manifest;
|
|
6750
|
+
}
|
|
6751
|
+
const asset = this.buildAsset(
|
|
6752
|
+
fileInfo,
|
|
6753
|
+
{
|
|
6754
|
+
photoId: null,
|
|
6755
|
+
preset: options?.preset,
|
|
6756
|
+
customWidths: options?.customWidths
|
|
6757
|
+
},
|
|
6758
|
+
manifest
|
|
6759
|
+
);
|
|
6760
|
+
emit("ready", 100, result.data.id);
|
|
6761
|
+
return {
|
|
6762
|
+
data: {
|
|
6763
|
+
file_id: result.data.id,
|
|
6764
|
+
photo_id: null,
|
|
6765
|
+
original_view_url: fileInfo.cdn_url ?? null,
|
|
6766
|
+
optimized_url_promise: Promise.resolve(manifest?.default ?? null),
|
|
6767
|
+
hls_url_promise: Promise.resolve(null),
|
|
6768
|
+
mime_type: mimeType,
|
|
6769
|
+
is_public: true,
|
|
6770
|
+
visibility: "anonymous_visible",
|
|
6771
|
+
cdn_url: fileInfo.cdn_url ?? null,
|
|
6772
|
+
asset
|
|
6773
|
+
},
|
|
6774
|
+
error: null
|
|
6775
|
+
};
|
|
6776
|
+
}
|
|
6777
|
+
async uploadImage(file, mimeType, visibility, gateOnPipeline, options, requestOptions, uploadOptions, emit) {
|
|
6778
|
+
if (visibility === "private") {
|
|
6779
|
+
const result = await this.photo.uploadViaStorage(file, uploadOptions, requestOptions);
|
|
6780
|
+
if (result.error || !result.data) {
|
|
6781
|
+
return { data: null, error: result.error };
|
|
6782
|
+
}
|
|
6783
|
+
if (gateOnPipeline) {
|
|
6784
|
+
emit("optimizing", 100, result.data.file_id);
|
|
6785
|
+
await result.data.optimized_url_promise;
|
|
6786
|
+
}
|
|
6787
|
+
const info2 = await this.fetchInfoBestEffort(result.data.file_id, requestOptions);
|
|
6788
|
+
const asset2 = this.buildAsset(
|
|
6789
|
+
info2 ?? fallbackFileInfo(result.data.file_id, mimeType, visibility, result.data.original_view_url, null),
|
|
6790
|
+
{
|
|
6791
|
+
photoId: result.data.photo_id,
|
|
6792
|
+
preset: options?.preset,
|
|
6793
|
+
customWidths: options?.customWidths
|
|
6794
|
+
}
|
|
6795
|
+
);
|
|
6796
|
+
emit("ready", 100, result.data.file_id);
|
|
6797
|
+
return {
|
|
6798
|
+
data: {
|
|
6799
|
+
file_id: result.data.file_id,
|
|
6800
|
+
photo_id: result.data.photo_id,
|
|
6801
|
+
original_view_url: result.data.original_view_url,
|
|
6802
|
+
optimized_url_promise: result.data.optimized_url_promise,
|
|
6803
|
+
hls_url_promise: Promise.resolve(null),
|
|
6804
|
+
mime_type: mimeType,
|
|
6805
|
+
is_public: false,
|
|
6806
|
+
visibility,
|
|
6807
|
+
cdn_url: null,
|
|
6808
|
+
asset: asset2
|
|
6809
|
+
},
|
|
6810
|
+
error: null
|
|
6811
|
+
};
|
|
6812
|
+
}
|
|
6813
|
+
const storageResult = await this.storage.upload(file, {
|
|
6814
|
+
...uploadOptions,
|
|
6815
|
+
visibility: "app_public",
|
|
6816
|
+
skipCompression: true
|
|
6817
|
+
});
|
|
6818
|
+
if (storageResult.error || !storageResult.data) {
|
|
6819
|
+
return { data: null, error: storageResult.error };
|
|
6820
|
+
}
|
|
6821
|
+
const originalViewUrl = storageResult.data.url ?? null;
|
|
6822
|
+
const registerResult = await this.photo.register({ fileId: storageResult.data.id }, requestOptions);
|
|
6823
|
+
const photoId = registerResult.error || !registerResult.data ? null : registerResult.data.id;
|
|
6824
|
+
const optimizedUrlPromise = photoId ? this.pollPhotoOptimizationComplete(photoId, requestOptions) : Promise.resolve(null);
|
|
6825
|
+
if (gateOnPipeline) {
|
|
6826
|
+
emit("optimizing", 100, storageResult.data.id);
|
|
6827
|
+
await optimizedUrlPromise;
|
|
6828
|
+
}
|
|
6829
|
+
const info = await this.fetchInfoBestEffort(storageResult.data.id, requestOptions) ?? storageResult.data;
|
|
6830
|
+
const asset = this.buildAsset(info, {
|
|
6831
|
+
photoId,
|
|
6832
|
+
preset: options?.preset,
|
|
6833
|
+
customWidths: options?.customWidths
|
|
6834
|
+
});
|
|
6835
|
+
emit("ready", 100, storageResult.data.id);
|
|
6836
|
+
return {
|
|
6837
|
+
data: {
|
|
6838
|
+
file_id: storageResult.data.id,
|
|
6839
|
+
photo_id: photoId,
|
|
6840
|
+
original_view_url: originalViewUrl,
|
|
6841
|
+
optimized_url_promise: optimizedUrlPromise,
|
|
6842
|
+
hls_url_promise: Promise.resolve(null),
|
|
6843
|
+
mime_type: mimeType,
|
|
6844
|
+
is_public: true,
|
|
6845
|
+
visibility,
|
|
6846
|
+
cdn_url: info.cdn_url ?? null,
|
|
6847
|
+
asset
|
|
6848
|
+
},
|
|
6849
|
+
error: null
|
|
6850
|
+
};
|
|
6851
|
+
}
|
|
6852
|
+
async waitForAnonymousReady(fileId, signal, emit) {
|
|
6853
|
+
const delays = [500, 1e3, 1500, 2e3, 2500, 3e3];
|
|
6854
|
+
for (let attempt = 0; attempt < 24; attempt++) {
|
|
6855
|
+
if (signal?.aborted) {
|
|
6856
|
+
return {
|
|
6857
|
+
cdnUrl: null,
|
|
6858
|
+
scanStatus: void 0,
|
|
6859
|
+
error: { code: "aborted", message: "Upload aborted", status: 0 }
|
|
6860
|
+
};
|
|
6861
|
+
}
|
|
6862
|
+
const status = await this.storage.getFileStatus(fileId);
|
|
6863
|
+
if (status.error || !status.data) {
|
|
6864
|
+
await sleep4(delays[Math.min(attempt, delays.length - 1)] ?? 3e3);
|
|
6865
|
+
continue;
|
|
6866
|
+
}
|
|
6867
|
+
const scanStatus = status.data.scan.status;
|
|
6868
|
+
if (status.data.urls.cdn_url) {
|
|
6869
|
+
return { cdnUrl: status.data.urls.cdn_url, scanStatus, error: null };
|
|
6870
|
+
}
|
|
6871
|
+
if (scanStatus === "threat" || scanStatus === "quarantined" || scanStatus === "error") {
|
|
6872
|
+
return {
|
|
6873
|
+
cdnUrl: null,
|
|
6874
|
+
scanStatus,
|
|
6875
|
+
error: scanError(scanStatus)
|
|
6876
|
+
};
|
|
6877
|
+
}
|
|
6878
|
+
emit("scanning", Math.min(99, 10 + attempt * 4), fileId);
|
|
6879
|
+
await sleep4(delays[Math.min(attempt, delays.length - 1)] ?? 3e3);
|
|
6880
|
+
}
|
|
6881
|
+
return {
|
|
6882
|
+
cdnUrl: null,
|
|
6883
|
+
scanStatus: "pending",
|
|
6884
|
+
error: {
|
|
6885
|
+
code: "file_scanning",
|
|
6886
|
+
message: "File scan did not complete in time. Re-fetch the file status and publish once cdn_url appears.",
|
|
6887
|
+
status: 202
|
|
6888
|
+
}
|
|
6889
|
+
};
|
|
6890
|
+
}
|
|
6891
|
+
async fetchInfoBestEffort(fileId, requestOptions) {
|
|
6892
|
+
const result = await this.storage.getInfo(fileId, requestOptions);
|
|
6893
|
+
return result.error || !result.data ? null : result.data;
|
|
6894
|
+
}
|
|
6895
|
+
async fetchManifest(file, options, requestOptions) {
|
|
6896
|
+
const kind = detectKind(file.content_type);
|
|
6897
|
+
const visibility = resolveFileVisibility(file);
|
|
6898
|
+
if (kind !== "image" || file.content_type === "image/svg+xml") {
|
|
6899
|
+
return this.buildManifest(file, options);
|
|
6900
|
+
}
|
|
6901
|
+
if (visibility !== "anonymous_visible" || options?.preset === "custom") {
|
|
6902
|
+
return this.buildManifest(file, options);
|
|
6903
|
+
}
|
|
6904
|
+
const persisted = await this.photo.getPublicManifest(
|
|
6905
|
+
file.id,
|
|
6906
|
+
{ preset: options?.preset ?? "inline" },
|
|
6907
|
+
requestOptions
|
|
6908
|
+
);
|
|
6909
|
+
if (persisted.error || !persisted.data) {
|
|
6910
|
+
return this.buildManifest(file, options);
|
|
6911
|
+
}
|
|
6912
|
+
return persisted.data;
|
|
6913
|
+
}
|
|
6914
|
+
async pollPhotoOptimizationComplete(photoId, requestOptions) {
|
|
6915
|
+
for (let attempt = 0; attempt < 40; attempt++) {
|
|
6916
|
+
const result = await this.photo.get(photoId, requestOptions);
|
|
6917
|
+
const status = result.data?.optimization_status;
|
|
6918
|
+
if (status === "completed") {
|
|
6919
|
+
return this.photo.getTransformUrl(photoId, { width: 1200 });
|
|
6920
|
+
}
|
|
6921
|
+
await sleep4(250);
|
|
6922
|
+
}
|
|
6923
|
+
return null;
|
|
6924
|
+
}
|
|
6925
|
+
async waitForAnonymousManifest(fileId, options, requestOptions, emit) {
|
|
6926
|
+
const preset = options?.preset ?? "inline";
|
|
6927
|
+
const delays = [500, 1e3, 1500, 2e3, 2500, 3e3];
|
|
6928
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
6929
|
+
if (options?.signal?.aborted) {
|
|
6930
|
+
return {
|
|
6931
|
+
manifest: null,
|
|
6932
|
+
error: { code: "aborted", message: "Upload aborted", status: 0 }
|
|
6933
|
+
};
|
|
6934
|
+
}
|
|
6935
|
+
const manifest = await this.photo.getPublicManifest(fileId, { preset }, requestOptions);
|
|
6936
|
+
if (!manifest.error && manifest.data && manifest.data.ready) {
|
|
6937
|
+
return { manifest: manifest.data, error: null };
|
|
6938
|
+
}
|
|
6939
|
+
emit("optimizing", Math.min(99, 15 + attempt * 3), fileId);
|
|
6940
|
+
await sleep4(delays[Math.min(attempt, delays.length - 1)] ?? 3e3);
|
|
6941
|
+
}
|
|
6942
|
+
return {
|
|
6943
|
+
manifest: null,
|
|
6944
|
+
error: {
|
|
6945
|
+
code: "manifest_pending",
|
|
6946
|
+
message: "Photo variants are still being generated. Re-fetch the public manifest once ready flips true.",
|
|
6947
|
+
status: 202
|
|
6948
|
+
}
|
|
6949
|
+
};
|
|
6950
|
+
}
|
|
6951
|
+
};
|
|
6952
|
+
function detectKind(contentType) {
|
|
6953
|
+
if (contentType.startsWith("image/")) return "image";
|
|
6954
|
+
if (contentType.startsWith("video/")) return "video";
|
|
6955
|
+
if (contentType.startsWith("audio/")) return "audio";
|
|
6956
|
+
if (contentType.includes("pdf") || contentType.includes("text/") || contentType.includes("word") || contentType.includes("sheet") || contentType.includes("presentation")) {
|
|
6957
|
+
return "document";
|
|
6958
|
+
}
|
|
6959
|
+
return "other";
|
|
6960
|
+
}
|
|
6961
|
+
function resolveVisibility(options) {
|
|
6962
|
+
if (options?.visibility) return options.visibility;
|
|
6963
|
+
if (options?.isPublic === true) return "app_public";
|
|
6964
|
+
return "private";
|
|
6965
|
+
}
|
|
6966
|
+
function resolveFileVisibility(file, fallback = "private") {
|
|
6967
|
+
if (file.visibility) return file.visibility;
|
|
6968
|
+
if (file.is_public === true) return "app_public";
|
|
6969
|
+
return fallback;
|
|
6970
|
+
}
|
|
6971
|
+
function shouldGateOnPipeline(policy) {
|
|
6972
|
+
return policy === "safe_public" || policy === "moderated" || policy === "compliance";
|
|
6973
|
+
}
|
|
6974
|
+
function resolvePresetSpec(preset, customWidths) {
|
|
6975
|
+
if (preset === "custom") {
|
|
6976
|
+
const widths = Array.from(
|
|
6977
|
+
new Set((customWidths ?? []).filter((value) => Number.isFinite(value) && value > 0))
|
|
6978
|
+
).sort((a, b) => a - b);
|
|
6979
|
+
return { widths: widths.length > 0 ? widths : MEDIA_PRESETS.inline.widths };
|
|
6980
|
+
}
|
|
6981
|
+
return MEDIA_PRESETS[preset];
|
|
6982
|
+
}
|
|
6983
|
+
function transformOptions(width, fit) {
|
|
6984
|
+
return fit === "cover" ? { width, height: width, fit: "cover" } : { width, fit: fit ?? "contain" };
|
|
6985
|
+
}
|
|
6986
|
+
function pickDefaultWidth(widths) {
|
|
6987
|
+
if (widths.includes(1200)) return 1200;
|
|
6988
|
+
return widths[Math.floor(widths.length / 2)] ?? widths[0] ?? 0;
|
|
6989
|
+
}
|
|
6990
|
+
function fallbackFileInfo(fileId, contentType, visibility, originalUrl, cdnUrl) {
|
|
6991
|
+
return {
|
|
6992
|
+
id: fileId,
|
|
6993
|
+
filename: fileId,
|
|
6994
|
+
content_type: contentType,
|
|
6995
|
+
size_bytes: 0,
|
|
6996
|
+
is_public: visibility !== "private",
|
|
6997
|
+
visibility,
|
|
6998
|
+
cdn_url: cdnUrl ?? void 0,
|
|
6999
|
+
url: originalUrl ?? void 0,
|
|
7000
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
7001
|
+
};
|
|
7002
|
+
}
|
|
7003
|
+
function normalizeThrownError(error) {
|
|
7004
|
+
if (typeof error === "object" && error && "code" in error && "message" in error) {
|
|
7005
|
+
return error;
|
|
7006
|
+
}
|
|
7007
|
+
return {
|
|
7008
|
+
code: "upload_error",
|
|
7009
|
+
message: error instanceof Error ? error.message : "Media operation failed",
|
|
7010
|
+
status: 500
|
|
7011
|
+
};
|
|
7012
|
+
}
|
|
7013
|
+
function scanError(status) {
|
|
7014
|
+
switch (status) {
|
|
7015
|
+
case "threat":
|
|
7016
|
+
return {
|
|
7017
|
+
code: "file_threat",
|
|
7018
|
+
message: "File was flagged as malicious during scanning.",
|
|
7019
|
+
status: 409
|
|
7020
|
+
};
|
|
7021
|
+
case "quarantined":
|
|
7022
|
+
return {
|
|
7023
|
+
code: "file_quarantined",
|
|
7024
|
+
message: "File was quarantined during scanning.",
|
|
7025
|
+
status: 409
|
|
7026
|
+
};
|
|
7027
|
+
default:
|
|
7028
|
+
return {
|
|
7029
|
+
code: "upload_error",
|
|
7030
|
+
message: "File scan failed.",
|
|
7031
|
+
status: 500
|
|
7032
|
+
};
|
|
7033
|
+
}
|
|
7034
|
+
}
|
|
7035
|
+
function sleep4(ms) {
|
|
7036
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7037
|
+
}
|
|
7038
|
+
|
|
6442
7039
|
// src/services/audio.ts
|
|
6443
7040
|
var AudioService = class extends ServiceModule {
|
|
6444
7041
|
/**
|
|
@@ -7387,6 +7984,7 @@ var ScaleMule = class {
|
|
|
7387
7984
|
this.functions = new FunctionsService(this._client);
|
|
7388
7985
|
this.photo = new PhotoService(this._client, this.storage);
|
|
7389
7986
|
this.audio = new AudioService(this._client, this.storage);
|
|
7987
|
+
this.media = new MediaService(this._client, this.storage, this.photo, this.video, this.audio);
|
|
7390
7988
|
this.tts = new TtsService(this._client);
|
|
7391
7989
|
this.flagContent = new FlagContentService(this._client);
|
|
7392
7990
|
this.creatorMaker = new CreatorMakerService(this._client);
|
|
@@ -7551,6 +8149,8 @@ export {
|
|
|
7551
8149
|
LeaderboardService,
|
|
7552
8150
|
ListingsService,
|
|
7553
8151
|
LoggerService,
|
|
8152
|
+
MEDIA_PRESETS,
|
|
8153
|
+
MediaService,
|
|
7554
8154
|
NotificationsService,
|
|
7555
8155
|
OrchestratorService,
|
|
7556
8156
|
PHONE_COUNTRIES,
|