fix(tapops): deteksi format file dan format tag

This commit is contained in:
Dodo
2026-06-04 18:53:39 +07:00
parent 36eebac312
commit 5c5c6aa42c
13 changed files with 548 additions and 144 deletions
+14 -5
View File
@@ -4,17 +4,26 @@ export class DirectusClient {
static API_BASE = 'https://api.ifuntanhub.dev';
static itemsUrl(collection) {
return `${this.API_BASE}/items/${collection}?limit=-1`;
const params = new URLSearchParams({
limit: '-1',
fields: '*,file.id,file.filename_download,file.type,file.filename_disk',
});
return `${this.API_BASE}/items/${collection}?${params.toString()}`;
}
static assetUrl(uuid) {
return `${this.API_BASE}/assets/${uuid}`;
static fileId(file) {
return file && typeof file === 'object' ? file.id : file;
}
static assetUrl(file) {
return `${this.API_BASE}/assets/${this.fileId(file)}`;
}
static viewerUrl(item) {
const type = DocumentFormatter.fileTypeFromName(item.nama_berkas);
const type = DocumentFormatter.fileInfoFromItem(item).type;
const params = new URLSearchParams({
id: item.file,
id: this.fileId(item.file),
name: item.nama_berkas,
type,
});
+15 -3
View File
@@ -1,7 +1,9 @@
export class FileDownloader {
static async download(url, filename) {
const downloadUrl = this.originalDownloadUrl(url);
try {
const response = await fetch(url);
const response = await fetch(downloadUrl);
if (!response.ok) throw new Error('Gagal mengunduh berkas.');
const blob = await response.blob();
const blobUrl = URL.createObjectURL(blob);
@@ -9,11 +11,21 @@ export class FileDownloader {
URL.revokeObjectURL(blobUrl);
} catch (err) {
console.warn('Unduh via Blob gagal, beralih ke parameter Directus:', err);
const fallbackUrl = url.includes('?') ? `${url}&download` : `${url}?download`;
this._click(fallbackUrl, filename);
this._click(downloadUrl, filename);
}
}
static originalDownloadUrl(url) {
if (!url) return '';
const hashIndex = url.indexOf('#');
const hash = hashIndex >= 0 ? url.slice(hashIndex) : '';
const base = hashIndex >= 0 ? url.slice(0, hashIndex) : url;
if (/(^|[?&])download(?:[=&]|$)/.test(base)) return url;
return `${base}${base.includes('?') ? '&' : '?'}download${hash}`;
}
static _click(href, filename) {
const a = document.createElement('a');
a.style.display = 'none';