Skip to content

Commit

Permalink
fix: adjust release download link
Browse files Browse the repository at this point in the history
  • Loading branch information
Xatsh committed Mar 20, 2024
1 parent bd750f9 commit fd8c75f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
27 changes: 27 additions & 0 deletions src/app/releases/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import { XMLParser } from 'fast-xml-parser'
import type { NextRequest } from 'next/server'

export async function GET(request: NextRequest, { params }: { params: { slug: string } }) {
const reqVersion = params.slug || 'latest'
const castPath = path.join(process.cwd(), 'content/releases', 'versions.xml')
const xml = await readFile(castPath, 'utf8')
const parser = new XMLParser()
const cast = parser.parse(xml)
const latest = `${cast.rss.channel.item[0]['sparkle:shortVersionString']}.${cast.rss.channel.item[0]['sparkle:version']}`
const version = reqVersion === 'latest' ? latest : reqVersion.replace('OpenCat-', '').replace('.dmg', '')
const buffer = await readFile(path.join(process.cwd(), 'content/releases', `OpenCat-${version}.dmg`))
return new Response(buffer, {
headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename="OpenCat-${version}.dmg"`,
},
})
return new Response(xml, {
headers: {
'Content-Type': 'application/xml',
},
})
}
28 changes: 3 additions & 25 deletions src/app/releases/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import { XMLParser } from 'fast-xml-parser'
import type { NextRequest } from 'next/server'
import { redirect } from 'next/navigation'

export async function GET(request: NextRequest) {
const reqVersion = request.url.split('/').pop() || 'latest'
const castPath = path.join(process.cwd(), 'content/releases', 'versions.xml')
const xml = await readFile(castPath, 'utf8')
const parser = new XMLParser()
const cast = parser.parse(xml)
const latest = `${cast.rss.channel.item[0]['sparkle:shortVersionString']}.${cast.rss.channel.item[0]['sparkle:version']}`
const version = reqVersion === 'latest' ? latest : reqVersion.replace('OpenCat-', '').replace('.dmg', '')
const buffer = await readFile(path.join(process.cwd(), 'content/releases', `OpenCat-${version}.dmg`))
return new Response(buffer, {
headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename="OpenCat-${version}.dmg"`,
},
})
return new Response(xml, {
headers: {
'Content-Type': 'application/xml',
},
})
export async function GET() {
redirect('/releases/latest')
}
2 changes: 1 addition & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function middleware(request: NextRequest) {
if (
[
'img',
'release',
'releases',
'docs',
// Add other folders/files in `public` that you want to ignore
].includes(pathname.split('/')[1])
Expand Down

0 comments on commit fd8c75f

Please sign in to comment.