-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include PR #1
- Loading branch information
Showing
7 changed files
with
90 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import path from 'path'; | ||
|
||
export function safePath(pathIn: string, options: { base: string, noSubDir?: boolean }): string | null { | ||
const base = options.base.endsWith('/') ? options.base : options.base + '/'; | ||
if (!pathIn.startsWith("./")) { | ||
pathIn = "./" + pathIn; | ||
} | ||
pathIn = path.join(base, pathIn); | ||
const normBase = path.normalize(base); | ||
const normPath = path.normalize(pathIn); | ||
|
||
if (normPath !== normBase && normPath.startsWith(normBase)) { | ||
if (options.noSubDir) { | ||
let rel = path.relative(normBase, normPath); | ||
if (rel.indexOf(path.sep) !== -1) { | ||
return null; | ||
} | ||
} | ||
return normPath; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,43 @@ | ||
import { safePath } from '$lib/server/safePath'; | ||
import { getCurrentFormattedDateTime } from '$lib/songUpdateTime'; | ||
import { json, error } from '@sveltejs/kit'; | ||
import fs from 'fs'; | ||
import type { RequestHandler } from './$types'; | ||
|
||
export const GET: RequestHandler = async ({ params }) => { | ||
const filePath = `./data/song/${params.id}.json`; | ||
if (!fs.existsSync(filePath)) { | ||
const filePath = safePath(`${params.id}.json`, { base: './data/song' }); | ||
if (!filePath) { | ||
return error(404, { | ||
message: "No correspoding song." | ||
}); | ||
} | ||
let data; | ||
try { data = fs.readFileSync(filePath); } catch (e) { | ||
return error(404, { | ||
message: "No corresponding song." | ||
}) | ||
}); | ||
} | ||
const data = fs.readFileSync(filePath); | ||
return json(JSON.parse(data.toString())); | ||
return json(JSON.parse(data.toString())); | ||
} | ||
|
||
export const POST: RequestHandler = async ({ request, params }) => { | ||
const timeStamp = new Date().getTime(); | ||
if (!fs.existsSync("./data/pending/")) { | ||
fs.mkdirSync("./data/pending"); | ||
try { | ||
if (!fs.existsSync("./data/pending/")) { | ||
fs.mkdirSync("./data/pending", { mode: 0o755 }); | ||
} | ||
const filePath = `./data/pending/${params.id}-${timeStamp}.json`; | ||
const data: MusicMetadata = await request.json(); | ||
data.updateTime = getCurrentFormattedDateTime(); | ||
fs.writeFileSync(filePath, JSON.stringify(data, null, 4), { mode: 0o644 }); | ||
return json({ | ||
"message": "successfully created" | ||
}, { | ||
status: 201 | ||
}); | ||
} catch (e) { | ||
return error(500, { | ||
message: "Internal server error." | ||
}); | ||
} | ||
const filePath = `./data/pending/${params.id}-${timeStamp}.json`; | ||
const data: MusicMetadata = await request.json(); | ||
data.updateTime = getCurrentFormattedDateTime(); | ||
fs.writeFileSync(filePath, JSON.stringify(data, null, 4)); | ||
return json({ | ||
"message": "successfully created" | ||
}, { | ||
status: 201 | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters