Seed create
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 = {
|
type CdnItem = {
|
||||||
name: string;
|
name: string;
|
||||||
path: string;
|
path: string;
|
||||||
cdnUrl: string;
|
cdnUrl: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type DirItem = {
|
type DirItem = {
|
||||||
type: "file" | "dir";
|
type: "file" | "dir";
|
||||||
name: string;
|
name: string;
|
||||||
};
|
};
|
||||||
|
const BASE_URL = "https://cld-dkr-makuro-seafile.wibudev.com";
|
||||||
// ✅ PAKAI ENV YANG BENAR
|
|
||||||
const BASE_URL = process.env.SEAFILE_URL!;
|
|
||||||
const REPO_ID = process.env.SEAFILE_REPO_ID!;
|
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";
|
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[]> {
|
async function getDirItems(): Promise<DirItem[]> {
|
||||||
if (!BASE_URL || !REPO_ID) {
|
|
||||||
throw new Error("SEAFILE env not configured correctly");
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = await getValidAuthToken();
|
const token = await getValidAuthToken();
|
||||||
|
const res = await fetch(
|
||||||
const url = `${BASE_URL}/api2/repos/${REPO_ID}/dir/?p=/${DIR_TARGET}`;
|
`${BASE_URL}/api2/repos/${REPO_ID}/dir/?p=/${DIR_TARGET}`,
|
||||||
|
{
|
||||||
const res = await fetch(url, {
|
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Token ${token}`,
|
Authorization: `Token ${token}`,
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
|
);
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
throw new Error(`Failed get dir items: ${text}`);
|
throw new Error(`Failed get dir items: ${text}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build PUBLIC CDN URL (DIRECTORY SHARE)
|
* Build PUBLIC CDN URL
|
||||||
*/
|
*/
|
||||||
function buildPublicCdnUrl(fileName: string) {
|
function buildPublicCdnUrl(fileName: string) {
|
||||||
const fullPath = `/${DIR_TARGET}/${fileName}`;
|
|
||||||
return `${BASE_URL}/d/${PUBLIC_SHARE_TOKEN}/files/?p=${encodeURIComponent(
|
return `${BASE_URL}/d/${PUBLIC_SHARE_TOKEN}/files/?p=${encodeURIComponent(
|
||||||
fullPath,
|
fileName,
|
||||||
)}&raw=1`;
|
)}&raw=1`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ambil semua PUBLIC CDN URL
|
* Ambil semua PUBLIC CDN URL
|
||||||
*/
|
*/
|
||||||
export async function getAllPublicCdnUrls(): Promise<CdnItem[]> {
|
export async function getAllPublicCdnUrls(): Promise<CdnItem[]> {
|
||||||
const items = await getDirItems();
|
const items = await getDirItems();
|
||||||
|
|
||||||
return items
|
return items
|
||||||
.filter((item) => item.type === "file")
|
.filter((item) => item.type === "file")
|
||||||
.map((file) => ({
|
.map((file) => {
|
||||||
|
const path = `${DIR_TARGET}/${file.name}`;
|
||||||
|
return {
|
||||||
name: file.name,
|
name: file.name,
|
||||||
path: `${DIR_TARGET}/${file.name}`,
|
path,
|
||||||
cdnUrl: buildPublicCdnUrl(file.name),
|
cdnUrl: buildPublicCdnUrl(file.name),
|
||||||
}));
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run langsung
|
* Run langsung (optional)
|
||||||
*/
|
*/
|
||||||
if (import.meta.main) {
|
if (import.meta.main) {
|
||||||
const data = await getAllPublicCdnUrls();
|
const data = await getAllPublicCdnUrls();
|
||||||
|
|||||||
Reference in New Issue
Block a user