diff --git a/prisma/lib/create_file_share_folder.ts b/prisma/lib/create_file_share_folder.ts index a9dabf84..80eea5b6 100644 --- a/prisma/lib/create_file_share_folder.ts +++ b/prisma/lib/create_file_share_folder.ts @@ -1,3 +1,100 @@ +// import { getValidAuthToken } from "../../src/lib/seafile-auth-service"; + +// type CdnItem = { +// name: string; +// path: string; +// cdnUrl: string; +// }; + +// type DirItem = { +// type: "file" | "dir"; +// name: string; +// }; + +// const BASE_URL = process.env.SEAFILE_BASE_URL!; +// const REPO_ID = process.env.SEAFILE_REPO_ID!; + +// // folder yang dishare (RELATIVE, tanpa slash depan) +// const DIR_TARGET = "asset-web"; + +// // 🔑 TOKEN DIRECTORY SHARE (/d/{token}) +// const PUBLIC_SHARE_TOKEN = process.env.SEAFILE_PUBLIC_SHARE_TOKEN!; + +// /** +// * Ambil list file dari repo (butuh token sekali) +// */ +// async function getDirItems(): Promise { +// const token = await getValidAuthToken(); + +// // Validasi bahwa semua variabel lingkungan telah diatur +// if (!BASE_URL) { +// throw new Error('SEAFILE_BASE_URL environment variable is not set'); +// } + +// if (!REPO_ID) { +// throw new Error('SEAFILE_REPO_ID environment variable is not set'); +// } + +// // Bangun URL dan pastikan valid +// const url = `${BASE_URL}/api2/repos/${REPO_ID}/dir/?p=/${DIR_TARGET}`; + +// try { +// new URL(url); // Ini akan melempar error jika URL tidak valid +// } catch (error) { +// throw new Error(`Invalid URL constructed: ${url}. Error: ${error}`); +// } + +// const res = await fetch(url, { +// headers: { +// Authorization: `Token ${token}`, +// }, +// }); + +// if (!res.ok) { +// const text = await res.text(); +// throw new Error(`Failed get dir items: ${text}`); +// } + +// return res.json(); +// } + +// /** +// * Build PUBLIC CDN URL +// */ +// function buildPublicCdnUrl(fileName: string) { +// return `${BASE_URL}/d/${PUBLIC_SHARE_TOKEN}/files/?p=${encodeURIComponent( +// fileName, +// )}&raw=1`; +// } + +// /** +// * Ambil semua PUBLIC CDN URL +// */ +// export async function getAllPublicCdnUrls(): Promise { +// const items = await getDirItems(); + +// return items +// .filter((item) => item.type === "file") +// .map((file) => { +// const path = `${DIR_TARGET}/${file.name}`; +// return { +// name: file.name, +// path, +// cdnUrl: buildPublicCdnUrl(file.name), +// }; +// }); +// } + +// /** +// * Run langsung (optional) +// */ +// if (import.meta.main) { +// const data = await getAllPublicCdnUrls(); +// console.log(data); +// } + + + import { getValidAuthToken } from "../../src/lib/seafile-auth-service"; type CdnItem = { @@ -11,29 +108,31 @@ type DirItem = { name: string; }; -const BASE_URL = process.env.SEAFILE_BASE_URL!; +// ✅ PAKAI ENV YANG BENAR +const BASE_URL = process.env.SEAFILE_URL!; const REPO_ID = process.env.SEAFILE_REPO_ID!; - -// folder yang dishare (RELATIVE, tanpa slash depan) -const DIR_TARGET = "asset-web"; - -// 🔑 TOKEN DIRECTORY SHARE (/d/{token}) const PUBLIC_SHARE_TOKEN = process.env.SEAFILE_PUBLIC_SHARE_TOKEN!; +// folder yang dishare (RELATIVE, TANPA slash depan) +const DIR_TARGET = "asset-web"; + /** - * Ambil list file dari repo (butuh token sekali) + * Ambil list file dari repo (token dipakai SEKALI) */ async function getDirItems(): Promise { + if (!BASE_URL || !REPO_ID) { + throw new Error("SEAFILE env not configured correctly"); + } + const token = await getValidAuthToken(); - const res = await fetch( - `${BASE_URL}/api2/repos/${REPO_ID}/dir/?p=/${DIR_TARGET}`, - { - headers: { - Authorization: `Token ${token}`, - }, + const url = `${BASE_URL}/api2/repos/${REPO_ID}/dir/?p=/${DIR_TARGET}`; + + const res = await fetch(url, { + headers: { + Authorization: `Token ${token}`, }, - ); + }); if (!res.ok) { const text = await res.text(); @@ -44,11 +143,12 @@ async function getDirItems(): Promise { } /** - * Build PUBLIC CDN URL + * Build PUBLIC CDN URL (DIRECTORY SHARE) */ function buildPublicCdnUrl(fileName: string) { + const fullPath = `/${DIR_TARGET}/${fileName}`; return `${BASE_URL}/d/${PUBLIC_SHARE_TOKEN}/files/?p=${encodeURIComponent( - fileName, + fullPath, )}&raw=1`; } @@ -60,20 +160,18 @@ export async function getAllPublicCdnUrls(): Promise { return items .filter((item) => item.type === "file") - .map((file) => { - const path = `${DIR_TARGET}/${file.name}`; - return { - name: file.name, - path, - cdnUrl: buildPublicCdnUrl(file.name), - }; - }); + .map((file) => ({ + name: file.name, + path: `${DIR_TARGET}/${file.name}`, + cdnUrl: buildPublicCdnUrl(file.name), + })); } /** - * Run langsung (optional) + * Run langsung */ if (import.meta.main) { const data = await getAllPublicCdnUrls(); console.log(data); } + diff --git a/x.json b/x.json index 4100b49d..c8875d01 100644 --- a/x.json +++ b/x.json @@ -1,26 +1,4 @@ [ - { - "type": "repo", - "id": "8814bfe1-30d5-4e77-ab36-3122fa59a022", - "owner": "0535ccb6211642c0a628f521577863a0@auth.local", - "owner_name": "nico", - "owner_contact_email": "nico@bip.com", - "name": "desa-darmasaba", - "mtime": 1769484147, - "modifier_email": "0535ccb6211642c0a628f521577863a0@auth.local", - "modifier_contact_email": "nico@bip.com", - "modifier_name": "nico", - "mtime_relative": "", - "size": 184351, - "size_formatted": "180.0\u00a0KB", - "encrypted": false, - "permission": "rw", - "virtual": false, - "root": "", - "head_commit_id": "253fd9604e54804a22cbf701bcc551e311e4a428", - "version": 1, - "salt": "" - }, { "type": "repo", "id": "f0e9ee4a-fd13-49a2-81c0-f253951d063a", @@ -28,18 +6,40 @@ "owner_name": "nico", "owner_contact_email": "nico@bip.com", "name": "My Library", - "mtime": 1769483609, + "mtime": 1770175246, "modifier_email": "0535ccb6211642c0a628f521577863a0@auth.local", "modifier_contact_email": "nico@bip.com", "modifier_name": "nico", - "mtime_relative": "", - "size": 19392875, - "size_formatted": "18.5\u00a0MB", + "mtime_relative": "", + "size": 8418588, + "size_formatted": "8.0\u00a0MB", "encrypted": false, "permission": "rw", "virtual": false, "root": "", - "head_commit_id": "22374e711577dcfb152fe07921458302feb9970a", + "head_commit_id": "d864f18510f72fbf7ed8601d89ced2cb92466057", + "version": 1, + "salt": "" + }, + { + "type": "repo", + "id": "8814bfe1-30d5-4e77-ab36-3122fa59a022", + "owner": "0535ccb6211642c0a628f521577863a0@auth.local", + "owner_name": "nico", + "owner_contact_email": "nico@bip.com", + "name": "desa-darmasaba", + "mtime": 1769486218, + "modifier_email": "0535ccb6211642c0a628f521577863a0@auth.local", + "modifier_contact_email": "nico@bip.com", + "modifier_name": "nico", + "mtime_relative": "", + "size": 5064580, + "size_formatted": "4.8\u00a0MB", + "encrypted": false, + "permission": "rw", + "virtual": false, + "root": "", + "head_commit_id": "1ddc57ecf377741d0b3abcf5a56a9ed0612c553c", "version": 1, "salt": "" }, @@ -48,12 +48,12 @@ "id": "9ef9d5e2-6df0-4635-ae1b-53ed52801524", "name": "RND", "owner": "Organization", - "mtime": 1768959408, - "mtime_relative": "", - "modifier_email": "dfa07fcb3e27453e969492855a2d6606@auth.local", - "modifier_contact_email": "jun@bip.com", - "modifier_name": "jun", - "size": 1197897275, + "mtime": 1770263243, + "mtime_relative": "", + "modifier_email": "ecb9357c59024141bc1731a3e10900ea@auth.local", + "modifier_contact_email": "inno@bip.com", + "modifier_name": "inno", + "size": 1198188885, "size_formatted": "1.1\u00a0GB", "encrypted": false, "permission": "rw", @@ -62,8 +62,8 @@ "share_from_contact_email": "wibu@bip.com", "share_type": "public", "root": "", - "head_commit_id": "5199e27babe4fd797e64beba62777a1dde58077b", + "head_commit_id": "f79f42f47c790bf5f14adb64bd644ec02aaf15bb", "version": 1, "salt": "" } -] \ No newline at end of file +] diff --git a/x.sh b/x.sh index d4a5e429..084fe164 100644 --- a/x.sh +++ b/x.sh @@ -1,23 +1,23 @@ -# ambil token -curl -X POST https://cld-dkr-makuro-seafile.wibudev.com/api2/auth-token/ \ - -d "username=nico@bip.com" \ - -d "password=Production_123" +# # ambil token +# curl -X POST https://cld-dkr-makuro-seafile.wibudev.com/api2/auth-token/ \ +# -d "username=nico@bip.com" \ +# -d "password=Production_123" -# ambil list repo / library +# # ambil list repo / library -TOKEN=20a19f4a04032215d50ce53292e6abdd38b9f806 +# TOKEN=20a19f4a04032215d50ce53292e6abdd38b9f806 # curl -X GET \ # https://cld-dkr-makuro-seafile.wibudev.com/api2/repos/ \ # -H "Authorization: Token $TOKEN" # REPO_NAME=desa-darmasaba -DIR_TARGET=asset-web +# DIR_TARGET=asset-web -REPO_ID=f0e9ee4a-fd13-49a2-81c0-f253951d063a -curl -X GET \ - "https://cld-dkr-makuro-seafile.wibudev.com/api2/repos/$REPO_ID/dir/?p=$DIR_TARGET" \ - -H "Authorization: Token $TOKEN" +# REPO_ID=f0e9ee4a-fd13-49a2-81c0-f253951d063a +# curl -X GET \ +# "https://cld-dkr-makuro-seafile.wibudev.com/api2/repos/$REPO_ID/dir/?p=$DIR_TARGET" \ +# -H "Authorization: Token $TOKEN" # curl -X GET \ # "https://cld-dkr-makuro-seafile.wibudev.com/api2/repos/$REPO_ID/file/?p=$DIR_TARGET/buku6.jpg" \