From e4546de377a5ec9889da627e43688a4736af00fd Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Tue, 21 May 2024 13:18:28 +0200 Subject: [PATCH] feat: always return serve urls --- packages/db/src/runtime/index.ts | 7 +++- packages/storage/src/codegen.ts | 53 ++++++++++++++------------ packages/storage/src/integration.ts | 59 +++++++++++++++-------------- packages/studio/src/core/utils.ts | 5 +++ 4 files changed, 70 insertions(+), 54 deletions(-) diff --git a/packages/db/src/runtime/index.ts b/packages/db/src/runtime/index.ts index 9174600820..782cd47853 100644 --- a/packages/db/src/runtime/index.ts +++ b/packages/db/src/runtime/index.ts @@ -61,8 +61,11 @@ const fileType = customType<{ data: string; driverData: string }>({ return value; }, fromDriver(value) { - // TODO: Should return a file object - return value; + // TODO: Get this from somewhere else + const studioUrl = + process.env.ASTRO_STUDIO_FILE_SERVER || 'https://studio.astro.build/api/serve_file'; + + return `${studioUrl}?id=${value}`; }, }); diff --git a/packages/storage/src/codegen.ts b/packages/storage/src/codegen.ts index 1674bd2b2d..ebebbcf7f7 100644 --- a/packages/storage/src/codegen.ts +++ b/packages/storage/src/codegen.ts @@ -1,9 +1,13 @@ -import type { AstroConfig } from 'astro'; import { existsSync } from 'node:fs'; import { mkdir, writeFile } from 'node:fs/promises'; -import { getAstroStudioStorageUrl } from './utils.js'; +import { + getAstroStudioFileServerUrl, + getProjectIdFromFile, + getSessionIdFromFile, +} from '@astrojs/studio'; +import type { AstroConfig } from 'astro'; import { STORAGE_CODE_FILE, STORAGE_TYPES_FILE } from './consts.js'; -import { getProjectIdFromFile, getSessionIdFromFile } from '@astrojs/studio'; +import { getAstroStudioStorageUrl } from './utils.js'; export async function codegen(astroConfig: Pick) { await codegenInternal({ root: astroConfig.root }); @@ -11,9 +15,10 @@ export async function codegen(astroConfig: Pick) { async function codegenInternal({ root }: { root: URL }) { const dotAstroDir = new URL('.astro/', root); + const serveUrl = getAstroStudioFileServerUrl(); - const images = await storageRequest('image'); - const all = await storageRequest('all'); + const images = await storageRequest('image', serveUrl); + const all = await storageRequest('all', serveUrl); const code = ` // This file is auto-generated by Astro Studio. Do not modify this file directly. @@ -30,7 +35,7 @@ async function codegenInternal({ root }: { root: URL }) { export function getFile(name: import("./${STORAGE_CODE_FILE}").File); export function getStudioImage(name: import("./${STORAGE_CODE_FILE}").Image); } - ` + `; if (!existsSync(dotAstroDir)) { await mkdir(dotAstroDir); @@ -40,35 +45,35 @@ async function codegenInternal({ root }: { root: URL }) { await writeFile(new URL(STORAGE_TYPES_FILE, dotAstroDir), types); } -async function storageRequest(fileKind: 'all' | 'image' = 'all') { +async function storageRequest(fileKind: 'all' | 'image' = 'all', serveUrl: string) { const projectId = await getProjectIdFromFile(); const linkUrl = getAstroStudioStorageUrl(); const sessionToken = await getSessionIdFromFile(); const response = await safeFetch( - linkUrl, - { - method: 'POST', - headers: { - Authorization: `Bearer ${sessionToken}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ projectId, fileKind }), - }, - (res) => { - // Unauthorized - if (res.status === 401) { - } - } + linkUrl, + { + method: 'POST', + headers: { + Authorization: `Bearer ${sessionToken}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ projectId, fileKind }), + }, + (res) => { + // Unauthorized + if (res.status === 401) { + } + } ); - return groupDataByName((await response.json()).data); + return rearrangeData((await response.json()).data, serveUrl); } -function groupDataByName(data: any) { +function rearrangeData(data: any, serveUrl: string) { const grouped: Record = {}; for (const item of data) { - grouped[item.name] = item; + grouped[item.name] = { ...item, serve_url: `${serveUrl}?id=${item.id}` }; } return grouped; diff --git a/packages/storage/src/integration.ts b/packages/storage/src/integration.ts index 028a2d7dd4..e1a7a26046 100644 --- a/packages/storage/src/integration.ts +++ b/packages/storage/src/integration.ts @@ -1,12 +1,12 @@ -import type { AstroConfig, AstroIntegration, AstroIntegrationLogger } from "astro"; -import { STORAGE_CODE_FILE, STORAGE_TYPES_FILE } from "./consts.js"; -import { codegen } from "./codegen.js"; -import { readFile, writeFile } from "node:fs/promises"; -import { bold } from "kleur/colors"; -import path from "node:path"; -import { fileURLToPath } from "url"; -import { normalizePath } from "vite"; -import { existsSync } from "node:fs"; +import { existsSync } from 'node:fs'; +import { readFile, writeFile } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'url'; +import type { AstroConfig, AstroIntegration, AstroIntegrationLogger } from 'astro'; +import { bold } from 'kleur/colors'; +import { normalizePath } from 'vite'; +import { codegen } from './codegen.js'; +import { STORAGE_CODE_FILE, STORAGE_TYPES_FILE } from './consts.js'; const VIRTUAL_MODULE_ID = 'astro:storage'; const RESOLVED_MODULE_ID = '\0' + 'astro:storage'; @@ -29,33 +29,36 @@ function vitePluginStorage({ root }: { root: URL }): VitePlugin { import { images, all } from '${new URL(STORAGE_CODE_FILE, dotAstroDir)}'; export function getFile(name) { - return all[name]; + return all[name].serve_url; } export function getStudioImage(name) { - return images[name].id; + return images[name].serve_url; } - ` - } - } + `; + }, + }; } export function storageIntegration(): AstroIntegration { -return { - name: "astro:studio", - hooks: { - 'astro:config:setup': async ({ config, updateConfig, logger }) => { - updateConfig({ - vite: { - plugins: [vitePluginStorage({ root: config.root }), vitePluginInjectEnvTs({ srcDir: config.srcDir, root: config.root }, logger)] - } - }) + return { + name: 'astro:studio', + hooks: { + 'astro:config:setup': async ({ config, updateConfig, logger }) => { + updateConfig({ + vite: { + plugins: [ + vitePluginStorage({ root: config.root }), + vitePluginInjectEnvTs({ srcDir: config.srcDir, root: config.root }, logger), + ], + }, + }); + }, + 'astro:config:done': async ({ config }) => { + await codegen({ root: config.root }); + }, }, - 'astro:config:done': async ({ config }) => { - await codegen({ root: config.root }); - } - } -} + }; } export function vitePluginInjectEnvTs( diff --git a/packages/studio/src/core/utils.ts b/packages/studio/src/core/utils.ts index 7cf40f7511..1e952fcdd3 100644 --- a/packages/studio/src/core/utils.ts +++ b/packages/studio/src/core/utils.ts @@ -9,3 +9,8 @@ export function getAstroStudioUrl(): string { const env = getAstroStudioEnv(); return env.ASTRO_STUDIO_URL || 'https://studio.astro.build'; } + +export function getAstroStudioFileServerUrl(): string { + const env = getAstroStudioEnv(); + return env.ASTRO_STUDIO_FILE_SERVER || 'https://studio.astro.build/api/serve_file'; +}