Fix image di seafile sudah tidak pakai token tapi by folder di seafile

Kasih console di page profil ppid & visi misi di Profile Desa
This commit is contained in:
2026-02-05 11:10:30 +08:00
parent 25000d0b0f
commit df154806f7
8 changed files with 274 additions and 12 deletions

View File

@@ -0,0 +1,79 @@
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 = "https://cld-dkr-makuro-seafile.wibudev.com";
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 = "3a9a9ecb5e244f4da8ae";
/**
* Ambil list file dari repo (butuh token sekali)
*/
async function getDirItems(): Promise<DirItem[]> {
const token = await getValidAuthToken();
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
*/
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<CdnItem[]> {
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);
}

View File

@@ -0,0 +1,71 @@
//ini code awal cari image by folder di seafile
type CdnItem = {
name: string;
path: string;
cdnUrl: string;
};
const BASE_URL = "https://cld-dkr-makuro-seafile.wibudev.com";
const SHARE_ID = "3325e9db2c504ebf9584";
// https://cld-dkr-makuro-seafile.wibudev.com/d/3a9a9ecb5e244f4da8ae/
// https://cld-dkr-makuro-seafile.wibudev.com/d/3a9a9ecb5e244f4da8ae/files/?p=-M_tICRVz6ZxOfvkuHQgU-mobile.webp&raw=1
/**
* Build CDN URL langsung (tanpa API, tanpa token)
*/
export function buildCdnUrl(filePath: string) {
// filePath contoh: "banner/home.jpg"
return `${BASE_URL}/f/${SHARE_ID}/${filePath}?raw=1`;
}
/**
* Ambil daftar file dari PUBLIC SHARE (optional)
* Tidak pakai token
*/
async function getPublicDirItems(path = "/"): Promise<any[]> {
const res = await fetch(
`${BASE_URL}/api/v2.1/share-links/${SHARE_ID}/dir/?p=${encodeURIComponent(
path,
)}`,
);
if (!res.ok) {
const text = await res.text();
throw new Error(`Failed get public dir items: ${text}`);
}
return res.json();
}
/**
* Ambil semua CDN URL dari folder public share
*/
export async function getAllCdnUrls(
dirPath = "/",
): Promise<CdnItem[]> {
const items = await getPublicDirItems(dirPath);
return items
.filter((item: any) => item.type === "file")
.map((file: any) => {
const filePath =
dirPath === "/"
? file.name
: `${dirPath.replace(/\/$/, "")}/${file.name}`;
return {
name: file.name,
path: filePath,
cdnUrl: buildCdnUrl(filePath),
};
});
}
if(import.meta.main) {
const allCdnUrls = await getAllCdnUrls();
console.log(allCdnUrls);
}

33
prisma/lib/get_sharef.ts Normal file
View File

@@ -0,0 +1,33 @@
const BASE_URL = "https://cld-dkr-makuro-seafile.wibudev.com";
const ADMIN_TOKEN = process.env.SEAFILE_TOKEN!;
const REPO_ID = process.env.SEAFILE_REPO_ID!;
export async function createFileShareForFolder() {
const res = await fetch(`${BASE_URL}/api/v2.1/share-links/`, {
method: "POST",
headers: {
Authorization: `Token ${ADMIN_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
repo_id: REPO_ID,
path: "/asset-web", // FOLDER
permission: "r",
}),
});
if (!res.ok) {
const text = await res.text();
throw new Error(text);
}
const data = await res.json();
console.log("FILE SHARE LINK:", data);
// data.link -> https://domain/f/XXXX/
// data.token / data.id (tergantung versi)
}
if (import.meta.main) {
await createFileShareForFolder();
}

View File

@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import prisma from "@/lib/prisma";
import { getAllDownloadUrls } from "./lib/get_images";
import { getAllPublicCdnUrls } from "./lib/create_file_share_folder";
export default async function seedAssets() {
const images = await getAllDownloadUrls();
const images = await getAllPublicCdnUrls();
for (const img of images) {
try {
@@ -20,7 +20,7 @@ export default async function seedAssets() {
name: img.name,
category: "image",
mimeType: "image/webp",
link: img.downloadUrl,
link: img.cdnUrl,
path: "images",
realName: img.name,
isActive: true,