@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.js
CHANGED
|
@@ -322,6 +322,8 @@ __export(index_exports, {
|
|
|
322
322
|
LeaderboardService: () => LeaderboardService,
|
|
323
323
|
ListingsService: () => ListingsService,
|
|
324
324
|
LoggerService: () => LoggerService,
|
|
325
|
+
MEDIA_PRESETS: () => MEDIA_PRESETS,
|
|
326
|
+
MediaService: () => MediaService,
|
|
325
327
|
NotificationsService: () => NotificationsService,
|
|
326
328
|
OrchestratorService: () => OrchestratorService,
|
|
327
329
|
PHONE_COUNTRIES: () => PHONE_COUNTRIES,
|
|
@@ -6564,6 +6566,15 @@ var PhotoService = class extends ServiceModule {
|
|
|
6564
6566
|
async delete(id, options) {
|
|
6565
6567
|
return this.del(`/${id}`, options);
|
|
6566
6568
|
}
|
|
6569
|
+
async getPresets(options) {
|
|
6570
|
+
return this._get("/presets", options);
|
|
6571
|
+
}
|
|
6572
|
+
async getManifest(id, params, options) {
|
|
6573
|
+
return this._get(this.withQuery(`/${id}/manifest`, params), options);
|
|
6574
|
+
}
|
|
6575
|
+
async getPublicManifest(id, params, options) {
|
|
6576
|
+
return this._get(this.withQuery(`/public/${id}/manifest`, params), options);
|
|
6577
|
+
}
|
|
6567
6578
|
/**
|
|
6568
6579
|
* Build an absolute URL for the on-demand transform endpoint.
|
|
6569
6580
|
*
|
|
@@ -6813,6 +6824,594 @@ var PhotoService = class extends ServiceModule {
|
|
|
6813
6824
|
}
|
|
6814
6825
|
};
|
|
6815
6826
|
|
|
6827
|
+
// src/services/media.ts
|
|
6828
|
+
var MEDIA_PRESETS = {
|
|
6829
|
+
hero: { widths: [400, 800, 1200, 1600, 2400] },
|
|
6830
|
+
inline: { widths: [400, 800, 1200] },
|
|
6831
|
+
thumbnail: { widths: [120, 240, 360], fit: "cover" },
|
|
6832
|
+
avatar: { widths: [64, 128, 256], fit: "cover" },
|
|
6833
|
+
logo: { widths: [120, 240, 480] }
|
|
6834
|
+
};
|
|
6835
|
+
var MediaService = class extends ServiceModule {
|
|
6836
|
+
constructor(client, storage, photo, video, audio) {
|
|
6837
|
+
super(client);
|
|
6838
|
+
this.storage = storage;
|
|
6839
|
+
this.photo = photo;
|
|
6840
|
+
this.video = video;
|
|
6841
|
+
this.audio = audio;
|
|
6842
|
+
this.basePath = "";
|
|
6843
|
+
}
|
|
6844
|
+
getPresets() {
|
|
6845
|
+
return MEDIA_PRESETS;
|
|
6846
|
+
}
|
|
6847
|
+
async upload(file, options, requestOptions) {
|
|
6848
|
+
const mimeType = file.type || "application/octet-stream";
|
|
6849
|
+
const kind = detectKind(mimeType);
|
|
6850
|
+
const visibility = resolveVisibility(options);
|
|
6851
|
+
const isPublic = visibility !== "private";
|
|
6852
|
+
const isAnonymous = visibility === "anonymous_visible";
|
|
6853
|
+
const gateOnPipeline = shouldGateOnPipeline(options?.policy);
|
|
6854
|
+
const emit = (phase, progress, fileId) => {
|
|
6855
|
+
options?.onProgress?.({ phase, progress, file_id: fileId });
|
|
6856
|
+
};
|
|
6857
|
+
const uploadOptions = {
|
|
6858
|
+
filename: options?.filename,
|
|
6859
|
+
metadata: options?.metadata,
|
|
6860
|
+
signal: options?.signal,
|
|
6861
|
+
onProgress: (progress) => emit("uploading", progress)
|
|
6862
|
+
};
|
|
6863
|
+
emit("presigning", 0);
|
|
6864
|
+
try {
|
|
6865
|
+
if (isAnonymous) {
|
|
6866
|
+
return await this.uploadAnonymous(file, mimeType, kind, options, requestOptions, uploadOptions, emit);
|
|
6867
|
+
}
|
|
6868
|
+
if (kind === "image" && !options?.skipPhotoRegister) {
|
|
6869
|
+
return await this.uploadImage(
|
|
6870
|
+
file,
|
|
6871
|
+
mimeType,
|
|
6872
|
+
visibility,
|
|
6873
|
+
gateOnPipeline,
|
|
6874
|
+
options,
|
|
6875
|
+
requestOptions,
|
|
6876
|
+
uploadOptions,
|
|
6877
|
+
emit
|
|
6878
|
+
);
|
|
6879
|
+
}
|
|
6880
|
+
if (kind === "video" && visibility === "private") {
|
|
6881
|
+
const result = await this.video.uploadViaStorage(file, uploadOptions, requestOptions);
|
|
6882
|
+
if (result.error || !result.data) {
|
|
6883
|
+
return { data: null, error: result.error };
|
|
6884
|
+
}
|
|
6885
|
+
if (gateOnPipeline) {
|
|
6886
|
+
emit("optimizing", 100, result.data.file_id);
|
|
6887
|
+
await result.data.hls_url_promise;
|
|
6888
|
+
}
|
|
6889
|
+
const info = await this.fetchInfoBestEffort(result.data.file_id, requestOptions);
|
|
6890
|
+
const asset2 = this.buildAsset(
|
|
6891
|
+
info ?? fallbackFileInfo(result.data.file_id, mimeType, visibility, result.data.original_view_url, null),
|
|
6892
|
+
{
|
|
6893
|
+
photoId: null,
|
|
6894
|
+
preset: options?.preset,
|
|
6895
|
+
customWidths: options?.customWidths
|
|
6896
|
+
}
|
|
6897
|
+
);
|
|
6898
|
+
emit("ready", 100, result.data.file_id);
|
|
6899
|
+
return {
|
|
6900
|
+
data: {
|
|
6901
|
+
file_id: result.data.file_id,
|
|
6902
|
+
photo_id: null,
|
|
6903
|
+
original_view_url: result.data.original_view_url,
|
|
6904
|
+
optimized_url_promise: Promise.resolve(null),
|
|
6905
|
+
hls_url_promise: result.data.hls_url_promise,
|
|
6906
|
+
mime_type: mimeType,
|
|
6907
|
+
is_public: false,
|
|
6908
|
+
visibility,
|
|
6909
|
+
cdn_url: null,
|
|
6910
|
+
asset: asset2
|
|
6911
|
+
},
|
|
6912
|
+
error: null
|
|
6913
|
+
};
|
|
6914
|
+
}
|
|
6915
|
+
if (kind === "audio" && visibility === "private") {
|
|
6916
|
+
const result = await this.audio.uploadViaStorage(file, uploadOptions, requestOptions);
|
|
6917
|
+
if (result.error || !result.data) {
|
|
6918
|
+
return { data: null, error: result.error };
|
|
6919
|
+
}
|
|
6920
|
+
const info = await this.fetchInfoBestEffort(result.data.file_id, requestOptions);
|
|
6921
|
+
const asset2 = this.buildAsset(
|
|
6922
|
+
info ?? fallbackFileInfo(result.data.file_id, mimeType, visibility, result.data.original_view_url, null),
|
|
6923
|
+
{
|
|
6924
|
+
photoId: null,
|
|
6925
|
+
preset: options?.preset,
|
|
6926
|
+
customWidths: options?.customWidths
|
|
6927
|
+
}
|
|
6928
|
+
);
|
|
6929
|
+
emit("ready", 100, result.data.file_id);
|
|
6930
|
+
return {
|
|
6931
|
+
data: {
|
|
6932
|
+
file_id: result.data.file_id,
|
|
6933
|
+
photo_id: null,
|
|
6934
|
+
original_view_url: result.data.original_view_url,
|
|
6935
|
+
optimized_url_promise: Promise.resolve(null),
|
|
6936
|
+
hls_url_promise: Promise.resolve(null),
|
|
6937
|
+
mime_type: mimeType,
|
|
6938
|
+
is_public: false,
|
|
6939
|
+
visibility,
|
|
6940
|
+
cdn_url: null,
|
|
6941
|
+
asset: asset2
|
|
6942
|
+
},
|
|
6943
|
+
error: null
|
|
6944
|
+
};
|
|
6945
|
+
}
|
|
6946
|
+
const storageResult = isPublic ? await this.storage.upload(file, {
|
|
6947
|
+
...uploadOptions,
|
|
6948
|
+
visibility: "app_public",
|
|
6949
|
+
skipCompression: true
|
|
6950
|
+
}) : await this.storage.uploadPrivate(file, uploadOptions);
|
|
6951
|
+
if (storageResult.error || !storageResult.data) {
|
|
6952
|
+
return { data: null, error: storageResult.error };
|
|
6953
|
+
}
|
|
6954
|
+
const fileInfo = storageResult.data;
|
|
6955
|
+
const asset = this.buildAsset(fileInfo, {
|
|
6956
|
+
photoId: null,
|
|
6957
|
+
preset: options?.preset,
|
|
6958
|
+
customWidths: options?.customWidths
|
|
6959
|
+
});
|
|
6960
|
+
emit("ready", 100, fileInfo.id);
|
|
6961
|
+
return {
|
|
6962
|
+
data: {
|
|
6963
|
+
file_id: fileInfo.id,
|
|
6964
|
+
photo_id: null,
|
|
6965
|
+
original_view_url: fileInfo.url ?? null,
|
|
6966
|
+
optimized_url_promise: Promise.resolve(null),
|
|
6967
|
+
hls_url_promise: Promise.resolve(null),
|
|
6968
|
+
mime_type: mimeType,
|
|
6969
|
+
is_public: fileInfo.is_public ?? isPublic,
|
|
6970
|
+
visibility: resolveFileVisibility(fileInfo, visibility),
|
|
6971
|
+
cdn_url: fileInfo.cdn_url ?? null,
|
|
6972
|
+
asset
|
|
6973
|
+
},
|
|
6974
|
+
error: null
|
|
6975
|
+
};
|
|
6976
|
+
} catch (error) {
|
|
6977
|
+
return {
|
|
6978
|
+
data: null,
|
|
6979
|
+
error: normalizeThrownError(error)
|
|
6980
|
+
};
|
|
6981
|
+
}
|
|
6982
|
+
}
|
|
6983
|
+
async get(fileId, options, requestOptions) {
|
|
6984
|
+
const result = await this.storage.getInfo(fileId, requestOptions);
|
|
6985
|
+
if (result.error || !result.data) {
|
|
6986
|
+
return { data: null, error: result.error };
|
|
6987
|
+
}
|
|
6988
|
+
return {
|
|
6989
|
+
data: this.buildAsset(result.data, {
|
|
6990
|
+
photoId: null,
|
|
6991
|
+
preset: options?.preset,
|
|
6992
|
+
customWidths: options?.customWidths
|
|
6993
|
+
}),
|
|
6994
|
+
error: null
|
|
6995
|
+
};
|
|
6996
|
+
}
|
|
6997
|
+
async list(params, requestOptions) {
|
|
6998
|
+
const result = await this.storage.list(params, requestOptions);
|
|
6999
|
+
return {
|
|
7000
|
+
data: result.data.map(
|
|
7001
|
+
(file) => this.buildAsset(file, {
|
|
7002
|
+
photoId: null,
|
|
7003
|
+
preset: params?.preset,
|
|
7004
|
+
customWidths: params?.customWidths
|
|
7005
|
+
})
|
|
7006
|
+
),
|
|
7007
|
+
metadata: result.metadata,
|
|
7008
|
+
error: result.error
|
|
7009
|
+
};
|
|
7010
|
+
}
|
|
7011
|
+
async delete(fileId, options) {
|
|
7012
|
+
try {
|
|
7013
|
+
const [, storageResult] = await Promise.all([
|
|
7014
|
+
this.photo.delete(fileId, options).catch(() => ({ data: null, error: null })),
|
|
7015
|
+
this.storage.delete(fileId, options)
|
|
7016
|
+
]);
|
|
7017
|
+
if (storageResult.error && storageResult.error.status !== 404) {
|
|
7018
|
+
return { data: null, error: storageResult.error };
|
|
7019
|
+
}
|
|
7020
|
+
return { data: { deleted: true }, error: null };
|
|
7021
|
+
} catch (error) {
|
|
7022
|
+
return { data: null, error: normalizeThrownError(error) };
|
|
7023
|
+
}
|
|
7024
|
+
}
|
|
7025
|
+
async releaseQuarantine(fileId, reason = "Released via @scalemule/sdk media.releaseQuarantine()", options) {
|
|
7026
|
+
const response = await this.client.post(
|
|
7027
|
+
`/v1/storage/admin/files/${fileId}/scan-override`,
|
|
7028
|
+
{ scan_status: "clean", reason },
|
|
7029
|
+
options
|
|
7030
|
+
);
|
|
7031
|
+
if (response.error) {
|
|
7032
|
+
return { data: null, error: response.error };
|
|
7033
|
+
}
|
|
7034
|
+
return this.get(fileId, void 0, options);
|
|
7035
|
+
}
|
|
7036
|
+
async getManifest(fileId, options, requestOptions) {
|
|
7037
|
+
const file = await this.storage.getInfo(fileId, requestOptions);
|
|
7038
|
+
if (file.error || !file.data) {
|
|
7039
|
+
return { data: null, error: file.error };
|
|
7040
|
+
}
|
|
7041
|
+
const manifest = await this.fetchManifest(file.data, options, requestOptions);
|
|
7042
|
+
return { data: manifest, error: null };
|
|
7043
|
+
}
|
|
7044
|
+
buildAsset(file, options, manifest) {
|
|
7045
|
+
const visibility = resolveFileVisibility(file);
|
|
7046
|
+
const photoId = options?.photoId ?? null;
|
|
7047
|
+
const originalUrl = visibility === "anonymous_visible" ? file.cdn_url ?? file.url ?? null : file.url ?? file.cdn_url ?? null;
|
|
7048
|
+
return {
|
|
7049
|
+
file_id: file.id,
|
|
7050
|
+
photo_id: photoId,
|
|
7051
|
+
filename: file.filename,
|
|
7052
|
+
content_type: file.content_type,
|
|
7053
|
+
size_bytes: file.size_bytes,
|
|
7054
|
+
kind: detectKind(file.content_type),
|
|
7055
|
+
visibility,
|
|
7056
|
+
is_public: file.is_public ?? visibility !== "private",
|
|
7057
|
+
scan_status: file.scan_status,
|
|
7058
|
+
created_at: file.created_at,
|
|
7059
|
+
original_url: originalUrl,
|
|
7060
|
+
cdn_url: file.cdn_url ?? null,
|
|
7061
|
+
manifest: manifest ?? this.buildManifest(file, options)
|
|
7062
|
+
};
|
|
7063
|
+
}
|
|
7064
|
+
buildManifest(file, options) {
|
|
7065
|
+
const kind = detectKind(file.content_type);
|
|
7066
|
+
const visibility = resolveFileVisibility(file);
|
|
7067
|
+
const originalUrl = visibility === "anonymous_visible" ? file.cdn_url ?? file.url ?? null : file.url ?? file.cdn_url ?? null;
|
|
7068
|
+
if (!originalUrl) {
|
|
7069
|
+
return null;
|
|
7070
|
+
}
|
|
7071
|
+
if (kind !== "image" || file.content_type === "image/svg+xml") {
|
|
7072
|
+
return {
|
|
7073
|
+
file_id: file.id,
|
|
7074
|
+
content_type: file.content_type,
|
|
7075
|
+
preset: "original",
|
|
7076
|
+
variants: { original: originalUrl },
|
|
7077
|
+
srcset: null,
|
|
7078
|
+
default: originalUrl
|
|
7079
|
+
};
|
|
7080
|
+
}
|
|
7081
|
+
const preset = options?.preset ?? "inline";
|
|
7082
|
+
const spec = resolvePresetSpec(preset, options?.customWidths);
|
|
7083
|
+
const variants = Object.fromEntries(
|
|
7084
|
+
spec.widths.map((width) => [
|
|
7085
|
+
String(width),
|
|
7086
|
+
visibility === "anonymous_visible" ? this.photo.getPublicTransformUrl(file.id, transformOptions(width, spec.fit)) : this.photo.getTransformUrl(file.id, transformOptions(width, spec.fit))
|
|
7087
|
+
])
|
|
7088
|
+
);
|
|
7089
|
+
const defaultWidth = pickDefaultWidth(spec.widths);
|
|
7090
|
+
const defaultUrl = variants[String(defaultWidth)] ?? Object.values(variants)[0] ?? originalUrl;
|
|
7091
|
+
return {
|
|
7092
|
+
file_id: file.id,
|
|
7093
|
+
content_type: file.content_type,
|
|
7094
|
+
preset,
|
|
7095
|
+
variants,
|
|
7096
|
+
srcset: spec.widths.map((width) => `${variants[String(width)]} ${width}w`).join(", "),
|
|
7097
|
+
default: defaultUrl
|
|
7098
|
+
};
|
|
7099
|
+
}
|
|
7100
|
+
async uploadAnonymous(file, mimeType, kind, options, requestOptions, uploadOptions, emit) {
|
|
7101
|
+
const result = await this.storage.uploadAnonymous(file, uploadOptions);
|
|
7102
|
+
if (result.error || !result.data) {
|
|
7103
|
+
return { data: null, error: result.error };
|
|
7104
|
+
}
|
|
7105
|
+
emit("scanning", 100, result.data.id);
|
|
7106
|
+
const ready = await this.waitForAnonymousReady(result.data.id, options?.signal, emit);
|
|
7107
|
+
if (ready.error) {
|
|
7108
|
+
return { data: null, error: ready.error };
|
|
7109
|
+
}
|
|
7110
|
+
const fileInfo = await this.fetchInfoBestEffort(result.data.id, requestOptions) ?? {
|
|
7111
|
+
...result.data,
|
|
7112
|
+
cdn_url: ready.cdnUrl ?? result.data.cdn_url,
|
|
7113
|
+
scan_status: ready.scanStatus
|
|
7114
|
+
};
|
|
7115
|
+
let manifest = this.buildManifest(fileInfo, {
|
|
7116
|
+
preset: options?.preset,
|
|
7117
|
+
customWidths: options?.customWidths
|
|
7118
|
+
});
|
|
7119
|
+
if (kind === "image" && fileInfo.content_type !== "image/svg+xml" && options?.preset !== "custom") {
|
|
7120
|
+
emit("optimizing", 100, result.data.id);
|
|
7121
|
+
const persistedManifest = await this.waitForAnonymousManifest(fileInfo.id, options, requestOptions, emit);
|
|
7122
|
+
if (persistedManifest.error) {
|
|
7123
|
+
return { data: null, error: persistedManifest.error };
|
|
7124
|
+
}
|
|
7125
|
+
manifest = persistedManifest.manifest ?? manifest;
|
|
7126
|
+
}
|
|
7127
|
+
const asset = this.buildAsset(
|
|
7128
|
+
fileInfo,
|
|
7129
|
+
{
|
|
7130
|
+
photoId: null,
|
|
7131
|
+
preset: options?.preset,
|
|
7132
|
+
customWidths: options?.customWidths
|
|
7133
|
+
},
|
|
7134
|
+
manifest
|
|
7135
|
+
);
|
|
7136
|
+
emit("ready", 100, result.data.id);
|
|
7137
|
+
return {
|
|
7138
|
+
data: {
|
|
7139
|
+
file_id: result.data.id,
|
|
7140
|
+
photo_id: null,
|
|
7141
|
+
original_view_url: fileInfo.cdn_url ?? null,
|
|
7142
|
+
optimized_url_promise: Promise.resolve(manifest?.default ?? null),
|
|
7143
|
+
hls_url_promise: Promise.resolve(null),
|
|
7144
|
+
mime_type: mimeType,
|
|
7145
|
+
is_public: true,
|
|
7146
|
+
visibility: "anonymous_visible",
|
|
7147
|
+
cdn_url: fileInfo.cdn_url ?? null,
|
|
7148
|
+
asset
|
|
7149
|
+
},
|
|
7150
|
+
error: null
|
|
7151
|
+
};
|
|
7152
|
+
}
|
|
7153
|
+
async uploadImage(file, mimeType, visibility, gateOnPipeline, options, requestOptions, uploadOptions, emit) {
|
|
7154
|
+
if (visibility === "private") {
|
|
7155
|
+
const result = await this.photo.uploadViaStorage(file, uploadOptions, requestOptions);
|
|
7156
|
+
if (result.error || !result.data) {
|
|
7157
|
+
return { data: null, error: result.error };
|
|
7158
|
+
}
|
|
7159
|
+
if (gateOnPipeline) {
|
|
7160
|
+
emit("optimizing", 100, result.data.file_id);
|
|
7161
|
+
await result.data.optimized_url_promise;
|
|
7162
|
+
}
|
|
7163
|
+
const info2 = await this.fetchInfoBestEffort(result.data.file_id, requestOptions);
|
|
7164
|
+
const asset2 = this.buildAsset(
|
|
7165
|
+
info2 ?? fallbackFileInfo(result.data.file_id, mimeType, visibility, result.data.original_view_url, null),
|
|
7166
|
+
{
|
|
7167
|
+
photoId: result.data.photo_id,
|
|
7168
|
+
preset: options?.preset,
|
|
7169
|
+
customWidths: options?.customWidths
|
|
7170
|
+
}
|
|
7171
|
+
);
|
|
7172
|
+
emit("ready", 100, result.data.file_id);
|
|
7173
|
+
return {
|
|
7174
|
+
data: {
|
|
7175
|
+
file_id: result.data.file_id,
|
|
7176
|
+
photo_id: result.data.photo_id,
|
|
7177
|
+
original_view_url: result.data.original_view_url,
|
|
7178
|
+
optimized_url_promise: result.data.optimized_url_promise,
|
|
7179
|
+
hls_url_promise: Promise.resolve(null),
|
|
7180
|
+
mime_type: mimeType,
|
|
7181
|
+
is_public: false,
|
|
7182
|
+
visibility,
|
|
7183
|
+
cdn_url: null,
|
|
7184
|
+
asset: asset2
|
|
7185
|
+
},
|
|
7186
|
+
error: null
|
|
7187
|
+
};
|
|
7188
|
+
}
|
|
7189
|
+
const storageResult = await this.storage.upload(file, {
|
|
7190
|
+
...uploadOptions,
|
|
7191
|
+
visibility: "app_public",
|
|
7192
|
+
skipCompression: true
|
|
7193
|
+
});
|
|
7194
|
+
if (storageResult.error || !storageResult.data) {
|
|
7195
|
+
return { data: null, error: storageResult.error };
|
|
7196
|
+
}
|
|
7197
|
+
const originalViewUrl = storageResult.data.url ?? null;
|
|
7198
|
+
const registerResult = await this.photo.register({ fileId: storageResult.data.id }, requestOptions);
|
|
7199
|
+
const photoId = registerResult.error || !registerResult.data ? null : registerResult.data.id;
|
|
7200
|
+
const optimizedUrlPromise = photoId ? this.pollPhotoOptimizationComplete(photoId, requestOptions) : Promise.resolve(null);
|
|
7201
|
+
if (gateOnPipeline) {
|
|
7202
|
+
emit("optimizing", 100, storageResult.data.id);
|
|
7203
|
+
await optimizedUrlPromise;
|
|
7204
|
+
}
|
|
7205
|
+
const info = await this.fetchInfoBestEffort(storageResult.data.id, requestOptions) ?? storageResult.data;
|
|
7206
|
+
const asset = this.buildAsset(info, {
|
|
7207
|
+
photoId,
|
|
7208
|
+
preset: options?.preset,
|
|
7209
|
+
customWidths: options?.customWidths
|
|
7210
|
+
});
|
|
7211
|
+
emit("ready", 100, storageResult.data.id);
|
|
7212
|
+
return {
|
|
7213
|
+
data: {
|
|
7214
|
+
file_id: storageResult.data.id,
|
|
7215
|
+
photo_id: photoId,
|
|
7216
|
+
original_view_url: originalViewUrl,
|
|
7217
|
+
optimized_url_promise: optimizedUrlPromise,
|
|
7218
|
+
hls_url_promise: Promise.resolve(null),
|
|
7219
|
+
mime_type: mimeType,
|
|
7220
|
+
is_public: true,
|
|
7221
|
+
visibility,
|
|
7222
|
+
cdn_url: info.cdn_url ?? null,
|
|
7223
|
+
asset
|
|
7224
|
+
},
|
|
7225
|
+
error: null
|
|
7226
|
+
};
|
|
7227
|
+
}
|
|
7228
|
+
async waitForAnonymousReady(fileId, signal, emit) {
|
|
7229
|
+
const delays = [500, 1e3, 1500, 2e3, 2500, 3e3];
|
|
7230
|
+
for (let attempt = 0; attempt < 24; attempt++) {
|
|
7231
|
+
if (signal?.aborted) {
|
|
7232
|
+
return {
|
|
7233
|
+
cdnUrl: null,
|
|
7234
|
+
scanStatus: void 0,
|
|
7235
|
+
error: { code: "aborted", message: "Upload aborted", status: 0 }
|
|
7236
|
+
};
|
|
7237
|
+
}
|
|
7238
|
+
const status = await this.storage.getFileStatus(fileId);
|
|
7239
|
+
if (status.error || !status.data) {
|
|
7240
|
+
await sleep4(delays[Math.min(attempt, delays.length - 1)] ?? 3e3);
|
|
7241
|
+
continue;
|
|
7242
|
+
}
|
|
7243
|
+
const scanStatus = status.data.scan.status;
|
|
7244
|
+
if (status.data.urls.cdn_url) {
|
|
7245
|
+
return { cdnUrl: status.data.urls.cdn_url, scanStatus, error: null };
|
|
7246
|
+
}
|
|
7247
|
+
if (scanStatus === "threat" || scanStatus === "quarantined" || scanStatus === "error") {
|
|
7248
|
+
return {
|
|
7249
|
+
cdnUrl: null,
|
|
7250
|
+
scanStatus,
|
|
7251
|
+
error: scanError(scanStatus)
|
|
7252
|
+
};
|
|
7253
|
+
}
|
|
7254
|
+
emit("scanning", Math.min(99, 10 + attempt * 4), fileId);
|
|
7255
|
+
await sleep4(delays[Math.min(attempt, delays.length - 1)] ?? 3e3);
|
|
7256
|
+
}
|
|
7257
|
+
return {
|
|
7258
|
+
cdnUrl: null,
|
|
7259
|
+
scanStatus: "pending",
|
|
7260
|
+
error: {
|
|
7261
|
+
code: "file_scanning",
|
|
7262
|
+
message: "File scan did not complete in time. Re-fetch the file status and publish once cdn_url appears.",
|
|
7263
|
+
status: 202
|
|
7264
|
+
}
|
|
7265
|
+
};
|
|
7266
|
+
}
|
|
7267
|
+
async fetchInfoBestEffort(fileId, requestOptions) {
|
|
7268
|
+
const result = await this.storage.getInfo(fileId, requestOptions);
|
|
7269
|
+
return result.error || !result.data ? null : result.data;
|
|
7270
|
+
}
|
|
7271
|
+
async fetchManifest(file, options, requestOptions) {
|
|
7272
|
+
const kind = detectKind(file.content_type);
|
|
7273
|
+
const visibility = resolveFileVisibility(file);
|
|
7274
|
+
if (kind !== "image" || file.content_type === "image/svg+xml") {
|
|
7275
|
+
return this.buildManifest(file, options);
|
|
7276
|
+
}
|
|
7277
|
+
if (visibility !== "anonymous_visible" || options?.preset === "custom") {
|
|
7278
|
+
return this.buildManifest(file, options);
|
|
7279
|
+
}
|
|
7280
|
+
const persisted = await this.photo.getPublicManifest(
|
|
7281
|
+
file.id,
|
|
7282
|
+
{ preset: options?.preset ?? "inline" },
|
|
7283
|
+
requestOptions
|
|
7284
|
+
);
|
|
7285
|
+
if (persisted.error || !persisted.data) {
|
|
7286
|
+
return this.buildManifest(file, options);
|
|
7287
|
+
}
|
|
7288
|
+
return persisted.data;
|
|
7289
|
+
}
|
|
7290
|
+
async pollPhotoOptimizationComplete(photoId, requestOptions) {
|
|
7291
|
+
for (let attempt = 0; attempt < 40; attempt++) {
|
|
7292
|
+
const result = await this.photo.get(photoId, requestOptions);
|
|
7293
|
+
const status = result.data?.optimization_status;
|
|
7294
|
+
if (status === "completed") {
|
|
7295
|
+
return this.photo.getTransformUrl(photoId, { width: 1200 });
|
|
7296
|
+
}
|
|
7297
|
+
await sleep4(250);
|
|
7298
|
+
}
|
|
7299
|
+
return null;
|
|
7300
|
+
}
|
|
7301
|
+
async waitForAnonymousManifest(fileId, options, requestOptions, emit) {
|
|
7302
|
+
const preset = options?.preset ?? "inline";
|
|
7303
|
+
const delays = [500, 1e3, 1500, 2e3, 2500, 3e3];
|
|
7304
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
7305
|
+
if (options?.signal?.aborted) {
|
|
7306
|
+
return {
|
|
7307
|
+
manifest: null,
|
|
7308
|
+
error: { code: "aborted", message: "Upload aborted", status: 0 }
|
|
7309
|
+
};
|
|
7310
|
+
}
|
|
7311
|
+
const manifest = await this.photo.getPublicManifest(fileId, { preset }, requestOptions);
|
|
7312
|
+
if (!manifest.error && manifest.data && manifest.data.ready) {
|
|
7313
|
+
return { manifest: manifest.data, error: null };
|
|
7314
|
+
}
|
|
7315
|
+
emit("optimizing", Math.min(99, 15 + attempt * 3), fileId);
|
|
7316
|
+
await sleep4(delays[Math.min(attempt, delays.length - 1)] ?? 3e3);
|
|
7317
|
+
}
|
|
7318
|
+
return {
|
|
7319
|
+
manifest: null,
|
|
7320
|
+
error: {
|
|
7321
|
+
code: "manifest_pending",
|
|
7322
|
+
message: "Photo variants are still being generated. Re-fetch the public manifest once ready flips true.",
|
|
7323
|
+
status: 202
|
|
7324
|
+
}
|
|
7325
|
+
};
|
|
7326
|
+
}
|
|
7327
|
+
};
|
|
7328
|
+
function detectKind(contentType) {
|
|
7329
|
+
if (contentType.startsWith("image/")) return "image";
|
|
7330
|
+
if (contentType.startsWith("video/")) return "video";
|
|
7331
|
+
if (contentType.startsWith("audio/")) return "audio";
|
|
7332
|
+
if (contentType.includes("pdf") || contentType.includes("text/") || contentType.includes("word") || contentType.includes("sheet") || contentType.includes("presentation")) {
|
|
7333
|
+
return "document";
|
|
7334
|
+
}
|
|
7335
|
+
return "other";
|
|
7336
|
+
}
|
|
7337
|
+
function resolveVisibility(options) {
|
|
7338
|
+
if (options?.visibility) return options.visibility;
|
|
7339
|
+
if (options?.isPublic === true) return "app_public";
|
|
7340
|
+
return "private";
|
|
7341
|
+
}
|
|
7342
|
+
function resolveFileVisibility(file, fallback = "private") {
|
|
7343
|
+
if (file.visibility) return file.visibility;
|
|
7344
|
+
if (file.is_public === true) return "app_public";
|
|
7345
|
+
return fallback;
|
|
7346
|
+
}
|
|
7347
|
+
function shouldGateOnPipeline(policy) {
|
|
7348
|
+
return policy === "safe_public" || policy === "moderated" || policy === "compliance";
|
|
7349
|
+
}
|
|
7350
|
+
function resolvePresetSpec(preset, customWidths) {
|
|
7351
|
+
if (preset === "custom") {
|
|
7352
|
+
const widths = Array.from(
|
|
7353
|
+
new Set((customWidths ?? []).filter((value) => Number.isFinite(value) && value > 0))
|
|
7354
|
+
).sort((a, b) => a - b);
|
|
7355
|
+
return { widths: widths.length > 0 ? widths : MEDIA_PRESETS.inline.widths };
|
|
7356
|
+
}
|
|
7357
|
+
return MEDIA_PRESETS[preset];
|
|
7358
|
+
}
|
|
7359
|
+
function transformOptions(width, fit) {
|
|
7360
|
+
return fit === "cover" ? { width, height: width, fit: "cover" } : { width, fit: fit ?? "contain" };
|
|
7361
|
+
}
|
|
7362
|
+
function pickDefaultWidth(widths) {
|
|
7363
|
+
if (widths.includes(1200)) return 1200;
|
|
7364
|
+
return widths[Math.floor(widths.length / 2)] ?? widths[0] ?? 0;
|
|
7365
|
+
}
|
|
7366
|
+
function fallbackFileInfo(fileId, contentType, visibility, originalUrl, cdnUrl) {
|
|
7367
|
+
return {
|
|
7368
|
+
id: fileId,
|
|
7369
|
+
filename: fileId,
|
|
7370
|
+
content_type: contentType,
|
|
7371
|
+
size_bytes: 0,
|
|
7372
|
+
is_public: visibility !== "private",
|
|
7373
|
+
visibility,
|
|
7374
|
+
cdn_url: cdnUrl ?? void 0,
|
|
7375
|
+
url: originalUrl ?? void 0,
|
|
7376
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
7377
|
+
};
|
|
7378
|
+
}
|
|
7379
|
+
function normalizeThrownError(error) {
|
|
7380
|
+
if (typeof error === "object" && error && "code" in error && "message" in error) {
|
|
7381
|
+
return error;
|
|
7382
|
+
}
|
|
7383
|
+
return {
|
|
7384
|
+
code: "upload_error",
|
|
7385
|
+
message: error instanceof Error ? error.message : "Media operation failed",
|
|
7386
|
+
status: 500
|
|
7387
|
+
};
|
|
7388
|
+
}
|
|
7389
|
+
function scanError(status) {
|
|
7390
|
+
switch (status) {
|
|
7391
|
+
case "threat":
|
|
7392
|
+
return {
|
|
7393
|
+
code: "file_threat",
|
|
7394
|
+
message: "File was flagged as malicious during scanning.",
|
|
7395
|
+
status: 409
|
|
7396
|
+
};
|
|
7397
|
+
case "quarantined":
|
|
7398
|
+
return {
|
|
7399
|
+
code: "file_quarantined",
|
|
7400
|
+
message: "File was quarantined during scanning.",
|
|
7401
|
+
status: 409
|
|
7402
|
+
};
|
|
7403
|
+
default:
|
|
7404
|
+
return {
|
|
7405
|
+
code: "upload_error",
|
|
7406
|
+
message: "File scan failed.",
|
|
7407
|
+
status: 500
|
|
7408
|
+
};
|
|
7409
|
+
}
|
|
7410
|
+
}
|
|
7411
|
+
function sleep4(ms) {
|
|
7412
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7413
|
+
}
|
|
7414
|
+
|
|
6816
7415
|
// src/services/audio.ts
|
|
6817
7416
|
var AudioService = class extends ServiceModule {
|
|
6818
7417
|
/**
|
|
@@ -7761,6 +8360,7 @@ var ScaleMule = class {
|
|
|
7761
8360
|
this.functions = new FunctionsService(this._client);
|
|
7762
8361
|
this.photo = new PhotoService(this._client, this.storage);
|
|
7763
8362
|
this.audio = new AudioService(this._client, this.storage);
|
|
8363
|
+
this.media = new MediaService(this._client, this.storage, this.photo, this.video, this.audio);
|
|
7764
8364
|
this.tts = new TtsService(this._client);
|
|
7765
8365
|
this.flagContent = new FlagContentService(this._client);
|
|
7766
8366
|
this.creatorMaker = new CreatorMakerService(this._client);
|
|
@@ -7926,6 +8526,8 @@ var index_default = ScaleMule;
|
|
|
7926
8526
|
LeaderboardService,
|
|
7927
8527
|
ListingsService,
|
|
7928
8528
|
LoggerService,
|
|
8529
|
+
MEDIA_PRESETS,
|
|
8530
|
+
MediaService,
|
|
7929
8531
|
NotificationsService,
|
|
7930
8532
|
OrchestratorService,
|
|
7931
8533
|
PHONE_COUNTRIES,
|