@@ -11,6 +11,7 @@ import { every } from "hono/combine";
11
11
import { hyperdriveMysql } from "./middleware/hyperdrive-mysql" ;
12
12
import { bearerAuth } from "hono/bearer-auth" ;
13
13
import { logger } from "hono/logger" ;
14
+ import { GET_LATEST_VERSION_SQL , VERIFY_LICENSE_SQL } from "./constants" ;
14
15
15
16
async function verifyTokenAndLicense ( token : string , c : Context ) {
16
17
const db = c . get ( 'db' ) ;
@@ -23,7 +24,7 @@ async function verifyTokenAndLicense(token: string, c: Context) {
23
24
// Verify license
24
25
const licenseId = payload . lic ;
25
26
const licenseKey = payload . secret ;
26
- const [ results ] = await db . query ( "SELECT * FROM licenses WHERE id = ? AND license_secret = ?;" , [ licenseId , licenseKey ] ) ;
27
+ const [ results ] = await db . query ( VERIFY_LICENSE_SQL , [ licenseId , licenseKey ] ) ;
27
28
return ( results as mysql . RowDataPacket [ ] ) . length === 1 ;
28
29
} catch ( err ) {
29
30
console . warn ( 'error verifying token' , err ) ;
@@ -88,6 +89,25 @@ app.use("/api/v2/convert/:sourceFormat/to/:targetFormat", (c, next) => {
88
89
} ) ;
89
90
//#endregion
90
91
92
+ //#region Download redirects
93
+ app . use ( "/download/*" , ( c , next ) => {
94
+ return hyperdriveMysql ( {
95
+ config : c . env . DB ,
96
+ } ) ( c , next ) ;
97
+ } ) ;
98
+ app . get ( "/download/:version/:packageName" , async ( c ) => {
99
+ let { version, packageName } = c . req . param ( ) ;
100
+ if ( version === 'latest' ) {
101
+ const db = c . get ( 'db' ) ;
102
+ if ( ! db ) throw new Error ( 'download controller must be used with (and sequenced after) the hyperdrive middleware' ) ;
103
+ const [ results ] = await db . query ( GET_LATEST_VERSION_SQL ) ;
104
+ const rows = results as mysql . RowDataPacket [ ] ;
105
+ version = `${ rows [ 0 ] . major } .${ rows [ 0 ] . minor } .${ rows [ 0 ] . revision } ` ;
106
+ }
107
+ return c . redirect ( `${ c . env . S3_BUCKET } /${ version } /${ packageName } ` ) ;
108
+ } ) ;
109
+ //#endregion
110
+
91
111
//#region All other requests
92
112
app . use ( forceRelativeRedirects ( ) ) ;
93
113
app . use ( async ( c , next ) => {
0 commit comments