Browse Source

console: use download proxy to rename mac and windows installers (#5697)

Co-authored-by: GitHub Action <[email protected]>
Brendan Allan 2 months ago
parent
commit
2a3a8a1ec2

+ 19 - 2
packages/console/app/src/routes/download/[platform].ts

@@ -1,20 +1,37 @@
 import { APIEvent } from "@solidjs/start"
+import { DownloadPlatform } from "./types"
 
 const assetNames: Record<string, string> = {
   "darwin-aarch64-dmg": "opencode-desktop-darwin-aarch64.dmg",
+  "darwin-x64-dmg": "opencode-desktop-darwin-x64.dmg",
   "windows-x64-nsis": "opencode-desktop-windows-x64.exe",
   "linux-x64-deb": "opencode-desktop-linux-amd64.deb",
   "linux-x64-rpm": "opencode-desktop-linux-x86_64.rpm",
-}
+} satisfies Record<DownloadPlatform, string>
+
+// Doing this on the server lets us preserve the original name for platforms we don't care to rename for
+const downloadNames: Record<string, string> = {
+  "darwin-aarch64-dmg": "OpenCode Desktop.dmg",
+  "darwin-x64-dmg": "OpenCode Desktop.dmg",
+  "windows-x64-nsis": "OpenCode Desktop Installer.exe",
+} satisfies { [K in DownloadPlatform]?: string }
 
 export async function GET({ params: { platform } }: APIEvent) {
   const assetName = assetNames[platform]
   if (!assetName) return new Response("Not Found", { status: 404 })
 
-  return await fetch(`https://github.com/sst/opencode/releases/latest/download/${assetName}`, {
+  const resp = await fetch(`https://github.com/sst/opencode/releases/latest/download/${assetName}`, {
     cf: {
+      // in case gh releases has rate limits
       cacheTtl: 60 * 60 * 24,
       cacheEverything: true,
     },
   } as any)
+
+  const downloadName = downloadNames[platform]
+
+  const headers = new Headers(resp.headers)
+  if (downloadName) headers.set("content-disposition", `attachment; filename="${downloadName}"`)
+
+  return new Response(resp.body, { ...resp, headers })
 }

+ 16 - 13
packages/console/app/src/routes/download/index.tsx

@@ -9,6 +9,7 @@ import desktopAppIcon from "../../asset/lander/opencode-desktop-icon.png"
 import { Legal } from "~/component/legal"
 import { config } from "~/config"
 import { createSignal, onMount, Show, JSX } from "solid-js"
+import { DownloadPlatform } from "./types"
 
 type OS = "macOS" | "Windows" | "Linux" | null
 
@@ -23,20 +24,23 @@ function detectOS(): OS {
   return null
 }
 
-function getDownloadUrl(os: OS): string {
-  const base = "https://github.com/sst/opencode/releases/latest/download"
+function getDownloadPlatform(os: OS): DownloadPlatform {
   switch (os) {
     case "macOS":
-      return `${base}/opencode-desktop-darwin-aarch64.dmg`
+      return "darwin-aarch64-dmg"
     case "Windows":
-      return `${base}/opencode-desktop-windows-x64.exe`
+      return "windows-x64-nsis"
     case "Linux":
-      return `${base}/opencode-desktop-linux-amd64.deb`
+      return "linux-x64-deb"
     default:
-      return `${base}/opencode-desktop-darwin-aarch64.dmg`
+      return "darwin-aarch64-dmg"
   }
 }
 
+function getDownloadHref(platform: DownloadPlatform) {
+  return `/download/${platform}`
+}
+
 function IconDownload(props: JSX.SvgSVGAttributes<SVGSVGElement>) {
   return (
     <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" {...props}>
@@ -60,7 +64,6 @@ function CopyStatus() {
 }
 
 export default function Download() {
-  const downloadUrl = "https://github.com/sst/opencode/releases/latest/download"
   const [detectedOS, setDetectedOS] = createSignal<OS>(null)
 
   onMount(() => {
@@ -92,7 +95,7 @@ export default function Download() {
               <h1>Download OpenCode</h1>
               <p>Available in Beta for macOS, Windows, and Linux</p>
               <Show when={detectedOS()}>
-                <a href={getDownloadUrl(detectedOS())} data-component="download-button">
+                <a href={getDownloadHref(getDownloadPlatform(detectedOS()))} data-component="download-button">
                   <IconDownload />
                   Download for {detectedOS()}
                 </a>
@@ -166,7 +169,7 @@ export default function Download() {
                     macOS (<span data-slot="hide-narrow">Apple </span>Silicon)
                   </span>
                 </div>
-                <a href={downloadUrl + "/opencode-desktop-darwin-aarch64.dmg"} data-component="action-button">
+                <a href={getDownloadHref("darwin-aarch64-dmg")} data-component="action-button">
                   Download
                 </a>
               </div>
@@ -182,7 +185,7 @@ export default function Download() {
                   </span>
                   <span>macOS (Intel)</span>
                 </div>
-                <a href={downloadUrl + "/opencode-desktop-darwin-x64.dmg"} data-component="action-button">
+                <a href={getDownloadHref("darwin-x64-dmg")} data-component="action-button">
                   Download
                 </a>
               </div>
@@ -205,7 +208,7 @@ export default function Download() {
                   </span>
                   <span>Windows (x64)</span>
                 </div>
-                <a href={downloadUrl + "/opencode-desktop-windows-x64.exe"} data-component="action-button">
+                <a href={getDownloadHref("windows-x64-nsis")} data-component="action-button">
                   Download
                 </a>
               </div>
@@ -221,7 +224,7 @@ export default function Download() {
                   </span>
                   <span>Linux (.deb)</span>
                 </div>
-                <a href={downloadUrl + "/opencode-desktop-linux-amd64.deb"} data-component="action-button">
+                <a href={getDownloadHref("linux-x64-deb")} data-component="action-button">
                   Download
                 </a>
               </div>
@@ -237,7 +240,7 @@ export default function Download() {
                   </span>
                   <span>Linux (.rpm)</span>
                 </div>
-                <a href={downloadUrl + "/opencode-desktop-linux-x86_64.rpm"} data-component="action-button">
+                <a href={getDownloadHref("linux-x64-rpm")} data-component="action-button">
                   Download
                 </a>
               </div>

+ 1 - 0
packages/console/app/src/routes/download/types.ts

@@ -0,0 +1 @@
+export type DownloadPlatform = `darwin-${"x64" | "aarch64"}-dmg` | "windows-x64-nsis" | `linux-x64-${"deb" | "rpm"}`