Skip to content

Commit ae548ad

Browse files
committed
I'm angry that cursor did this correctly
1 parent aa12498 commit ae548ad

File tree

5 files changed

+441
-117
lines changed

5 files changed

+441
-117
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { STORAGE_PATH } from './config.server'
2+
import fs from 'fs/promises'
3+
import path from 'path'
4+
5+
async function fileExists(filename: string) {
6+
try {
7+
await fs.access(filename, fs.constants.R_OK) // check file exists and is readable
8+
return true
9+
} catch (err) {
10+
return false
11+
}
12+
}
13+
14+
function safeJoin(base: string, file: string) {
15+
const file2 = path.join('/', file) // removes all `../` by making the file relative to root `/`
16+
return path.join(base, file2)
17+
}
18+
19+
export default async function processFileParam(
20+
request: Request,
21+
param: string,
22+
) {
23+
const _file = new URL(request.url).searchParams.get(param) as string
24+
25+
if (!_file) {
26+
throw new Response('missing ?file param', { status: 400 })
27+
}
28+
29+
const file = safeJoin(STORAGE_PATH, _file)
30+
31+
if (!fileExists(file)) {
32+
throw new Response('file not found', { status: 404 })
33+
}
34+
35+
return file
36+
}

app/lib/unzip-cbz.server.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from "fs/promises"
2+
import JSZip from "jszip"
3+
4+
export async function unzipCbz(path: string) {
5+
const buf = await fs.readFile(path)
6+
const zip = new JSZip()
7+
await zip.loadAsync(buf as Uint8Array)
8+
const promises = [] as Promise<string>[]
9+
zip.forEach((file) => {
10+
const f = zip.file(file)
11+
if (f) {
12+
promises.push(f.async('base64'))
13+
}
14+
})
15+
const files = await Promise.all(promises)
16+
return files.map((f, i) => {
17+
return {
18+
name: Object.keys(zip.files)[i],
19+
base64: f,
20+
}
21+
})
22+
}

0 commit comments

Comments
 (0)