This commit is contained in:
alorig
2025-11-18 05:03:27 +05:00
parent 342d9eab17
commit 40d379dd7e
35 changed files with 2073 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
/**
* File Access Utility
* Phase 5: Sites Renderer & Publishing
*
* Integrates with Phase 3's SiteBuilderFileService for file access.
* Provides utilities to access site assets (images, documents, media).
*/
const SITES_DATA_PATH = import.meta.env.SITES_DATA_PATH || '/sites';
const API_URL = import.meta.env.VITE_API_URL || 'https://api.igny8.com/api';
/**
* Get file URL for a site asset.
*
* @param siteId - Site ID
* @param version - Site version (optional, defaults to 'latest')
* @param filePath - Relative path to file from assets directory
* @returns Full URL to the asset
*/
export function getSiteAssetUrl(
siteId: string | number,
filePath: string,
version: string | number = 'latest'
): string {
// Try filesystem first (for deployed sites)
const fsPath = `${SITES_DATA_PATH}/clients/${siteId}/v${version}/assets/${filePath}`;
// In browser, we need to use API endpoint
// The backend will serve files from the filesystem
return `${API_URL}/v1/site-builder/assets/${siteId}/${version}/${filePath}`;
}
/**
* Get image URL for a site.
*/
export function getSiteImageUrl(
siteId: string | number,
imagePath: string,
version: string | number = 'latest'
): string {
return getSiteAssetUrl(siteId, `images/${imagePath}`, version);
}
/**
* Get document URL for a site.
*/
export function getSiteDocumentUrl(
siteId: string | number,
documentPath: string,
version: string | number = 'latest'
): string {
return getSiteAssetUrl(siteId, `documents/${documentPath}`, version);
}
/**
* Get media URL for a site.
*/
export function getSiteMediaUrl(
siteId: string | number,
mediaPath: string,
version: string | number = 'latest'
): string {
return getSiteAssetUrl(siteId, `media/${mediaPath}`, version);
}
/**
* Check if a file exists.
*
* @param siteId - Site ID
* @param filePath - Relative path to file
* @param version - Site version
* @returns Promise that resolves to true if file exists
*/
export async function fileExists(
siteId: string | number,
filePath: string,
version: string | number = 'latest'
): Promise<boolean> {
try {
const url = getSiteAssetUrl(siteId, filePath, version);
const response = await fetch(url, { method: 'HEAD' });
return response.ok;
} catch {
return false;
}
}
/**
* Load file content as text.
*/
export async function loadFileAsText(
siteId: string | number,
filePath: string,
version: string | number = 'latest'
): Promise<string> {
const url = getSiteAssetUrl(siteId, filePath, version);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to load file: ${filePath}`);
}
return response.text();
}
/**
* Load file content as blob.
*/
export async function loadFileAsBlob(
siteId: string | number,
filePath: string,
version: string | number = 'latest'
): Promise<Blob> {
const url = getSiteAssetUrl(siteId, filePath, version);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to load file: ${filePath}`);
}
return response.blob();
}