feat: backend implementation for media manager v2 (WIP - Undeployed)

This commit is contained in:
Ben Miller
2025-12-28 08:13:27 -07:00
parent a9cb63fd67
commit 6e1222cec9
11 changed files with 763 additions and 189 deletions

View File

@ -38,45 +38,45 @@ export function getPickerConfig() {
}
export function getMediaForSku(sku: string): any[] {
const config = new Config()
const driveService = new GASDriveService()
const shop = new Shop()
const shopifyMediaService = new ShopifyMediaService(shop)
const networkService = new GASNetworkService()
const mediaService = new MediaService(driveService, shopifyMediaService, networkService, config)
try {
const config = new Config() // Moved inside try block to catch init errors
const folder = driveService.getOrCreateFolder(sku, config.productPhotosFolderId)
const files = driveService.getFiles(folder.getId())
// Resolve Product ID (Best Effort)
const product = new Product(sku)
const shopifyId = product.shopify_id || ""
return files.map(f => {
let thumb = ""
try {
const bytes = f.getThumbnail().getBytes()
thumb = "data:image/png;base64," + Utilities.base64Encode(bytes)
} catch (e) {
console.log(`Failed to get thumbnail for ${f.getName()}`)
// Fallback or empty
}
return {
id: f.getId(),
name: f.getName(),
thumbnailLink: thumb
}
})
} catch (e) {
console.error(e)
return []
return mediaService.getUnifiedMediaState(sku, shopifyId)
}
export function saveMediaChanges(sku: string, finalState: any[]) {
const config = new Config()
const driveService = new GASDriveService()
const shop = new Shop()
const shopifyMediaService = new ShopifyMediaService(shop)
const networkService = new GASNetworkService()
const mediaService = new MediaService(driveService, shopifyMediaService, networkService, config)
const product = new Product(sku)
if (!product.shopify_id) {
// Allow saving Drive-only changes? No, we need Shopify context for "Staging" usually.
// But if we just rename drive files, we could?
// For now, fail safe.
throw new Error("Product must be synced to Shopify before saving media changes.")
}
mediaService.processMediaChanges(sku, finalState, product.shopify_id)
}
export function saveFileToDrive(sku: string, filename: string, mimeType: string, base64Data: string) {
const config = new Config()
const driveService = new GASDriveService()
const folder = driveService.getOrCreateFolder(sku, config.productPhotosFolderId)
const blob = Utilities.newBlob(Utilities.base64Decode(base64Data), mimeType, filename)
driveService.saveFile(blob, folder.getId())
// Auto-sync after upload?
// syncMediaForSku(sku) // Optional: auto-sync
}
// Picker Callback specific handler if needed, or we just rely on frontend passing back file ID
@ -205,31 +205,7 @@ export function importFromPicker(sku: string, fileId: string, mimeType: string,
}
export function syncMediaForSku(sku: string) {
const config = new Config()
const driveService = new GASDriveService()
const shop = new Shop()
const shopifyMediaService = new ShopifyMediaService(shop)
const networkService = new GASNetworkService()
const mediaService = new MediaService(driveService, shopifyMediaService, networkService, config)
// Need Shopify Product ID
// We can get it from the Product class or Sheet
const product = new Product(sku)
if (!product.shopify_id) {
product.MatchToShopifyProduct(shop)
}
if (!product.shopify_id) {
throw new Error("Product not found on Shopify. Please sync product first.")
}
mediaService.syncMediaForSku(sku, product.shopify_id)
// Update thumbnail in sheet
// TODO: Implement thumbnail update in sheet if desired
}
export function debugScopes() {
const token = ScriptApp.getOAuthToken();