Merge pull request 'Seed create' (#65) from nico/5-feb-26-1 into staggingweb
Reviewed-on: http://wibugit.wibudev.com/wibu/desa-darmasaba/pulls/65
This commit is contained in:
@@ -95,80 +95,148 @@
|
||||
|
||||
|
||||
|
||||
import { getValidAuthToken } from "../../src/lib/seafile-auth-service";
|
||||
// import { getValidAuthToken } from "../../src/lib/seafile-auth-service";
|
||||
|
||||
// type CdnItem = {
|
||||
// name: string;
|
||||
// path: string;
|
||||
// cdnUrl: string;
|
||||
// };
|
||||
|
||||
// type DirItem = {
|
||||
// type: "file" | "dir";
|
||||
// name: string;
|
||||
// };
|
||||
|
||||
// // ✅ PAKAI ENV YANG BENAR
|
||||
// const BASE_URL = process.env.SEAFILE_URL!;
|
||||
// const REPO_ID = process.env.SEAFILE_REPO_ID!;
|
||||
// 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 (token dipakai SEKALI)
|
||||
// */
|
||||
// async function getDirItems(): Promise<DirItem[]> {
|
||||
// if (!BASE_URL || !REPO_ID) {
|
||||
// throw new Error("SEAFILE env not configured correctly");
|
||||
// }
|
||||
|
||||
// const token = await getValidAuthToken();
|
||||
|
||||
// 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();
|
||||
// throw new Error(`Failed get dir items: ${text}`);
|
||||
// }
|
||||
|
||||
// return res.json();
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 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(
|
||||
// fullPath,
|
||||
// )}&raw=1`;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Ambil semua PUBLIC CDN URL
|
||||
// */
|
||||
// export async function getAllPublicCdnUrls(): Promise<CdnItem[]> {
|
||||
// const items = await getDirItems();
|
||||
|
||||
// return items
|
||||
// .filter((item) => item.type === "file")
|
||||
// .map((file) => ({
|
||||
// name: file.name,
|
||||
// path: `${DIR_TARGET}/${file.name}`,
|
||||
// cdnUrl: buildPublicCdnUrl(file.name),
|
||||
// }));
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Run langsung
|
||||
// */
|
||||
// if (import.meta.main) {
|
||||
// const data = await getAllPublicCdnUrls();
|
||||
// console.log(data);
|
||||
// }
|
||||
|
||||
|
||||
import { getValidAuthToken } from "../../src/lib/seafile-auth-service";
|
||||
type CdnItem = {
|
||||
name: string;
|
||||
path: string;
|
||||
cdnUrl: string;
|
||||
};
|
||||
|
||||
type DirItem = {
|
||||
type: "file" | "dir";
|
||||
name: string;
|
||||
};
|
||||
|
||||
// ✅ PAKAI ENV YANG BENAR
|
||||
const BASE_URL = process.env.SEAFILE_URL!;
|
||||
const BASE_URL = "https://cld-dkr-makuro-seafile.wibudev.com";
|
||||
const REPO_ID = process.env.SEAFILE_REPO_ID!;
|
||||
const PUBLIC_SHARE_TOKEN = process.env.SEAFILE_PUBLIC_SHARE_TOKEN!;
|
||||
|
||||
// folder yang dishare (RELATIVE, TANPA slash depan)
|
||||
// folder yang dishare (RELATIVE, tanpa slash depan)
|
||||
const DIR_TARGET = "asset-web";
|
||||
|
||||
// 🔑 TOKEN DIRECTORY SHARE (/d/{token})
|
||||
const PUBLIC_SHARE_TOKEN = "3a9a9ecb5e244f4da8ae";
|
||||
/**
|
||||
* Ambil list file dari repo (token dipakai SEKALI)
|
||||
* Ambil list file dari repo (butuh token sekali)
|
||||
*/
|
||||
async function getDirItems(): Promise<DirItem[]> {
|
||||
if (!BASE_URL || !REPO_ID) {
|
||||
throw new Error("SEAFILE env not configured correctly");
|
||||
}
|
||||
|
||||
const token = await getValidAuthToken();
|
||||
|
||||
const url = `${BASE_URL}/api2/repos/${REPO_ID}/dir/?p=/${DIR_TARGET}`;
|
||||
|
||||
const res = await fetch(url, {
|
||||
headers: {
|
||||
Authorization: `Token ${token}`,
|
||||
const res = await fetch(
|
||||
`${BASE_URL}/api2/repos/${REPO_ID}/dir/?p=/${DIR_TARGET}`,
|
||||
{
|
||||
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 (DIRECTORY SHARE)
|
||||
* Build PUBLIC CDN URL
|
||||
*/
|
||||
function buildPublicCdnUrl(fileName: string) {
|
||||
const fullPath = `/${DIR_TARGET}/${fileName}`;
|
||||
return `${BASE_URL}/d/${PUBLIC_SHARE_TOKEN}/files/?p=${encodeURIComponent(
|
||||
fullPath,
|
||||
fileName,
|
||||
)}&raw=1`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ambil semua PUBLIC CDN URL
|
||||
*/
|
||||
export async function getAllPublicCdnUrls(): Promise<CdnItem[]> {
|
||||
const items = await getDirItems();
|
||||
|
||||
return items
|
||||
.filter((item) => item.type === "file")
|
||||
.map((file) => ({
|
||||
name: file.name,
|
||||
path: `${DIR_TARGET}/${file.name}`,
|
||||
cdnUrl: buildPublicCdnUrl(file.name),
|
||||
}));
|
||||
.map((file) => {
|
||||
const path = `${DIR_TARGET}/${file.name}`;
|
||||
return {
|
||||
name: file.name,
|
||||
path,
|
||||
cdnUrl: buildPublicCdnUrl(file.name),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run langsung
|
||||
* Run langsung (optional)
|
||||
*/
|
||||
if (import.meta.main) {
|
||||
const data = await getAllPublicCdnUrls();
|
||||
|
||||
Reference in New Issue
Block a user