fs robustness patch
Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
This commit is contained in:
parent
aef964aa35
commit
63c65e8b7d
@ -1,4 +1,5 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { globalMemoryStorage, songData, songNameCache } from '$lib/server/cache.js';
|
||||
import { getDirectoryHash } from '../dirHash';
|
||||
|
||||
@ -25,7 +26,7 @@ export async function loadData() {
|
||||
songNameCache.flushAll();
|
||||
for (const songID of songList) {
|
||||
try {
|
||||
const fileContentString = fs.readFileSync(dataPath + songID + '.json').toString();
|
||||
const fileContentString = fs.readFileSync(path.join(dataPath, songID + '.json')).toString();
|
||||
const data = JSON.parse(fileContentString);
|
||||
songData.set(songID, data);
|
||||
const metadata: MusicMetadata = data;
|
||||
|
@ -12,7 +12,7 @@ export function getDirectoryHash(dir: string): string {
|
||||
|
||||
files.forEach(file => {
|
||||
const filePath = path.join(currentDir, file);
|
||||
const stats = fs.statSync(filePath);
|
||||
const stats = fs.lstatSync(filePath);
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
traverseDirectory(filePath);
|
||||
@ -30,7 +30,7 @@ export function getDirectoryHash(dir: string): string {
|
||||
|
||||
// Create hash from file details
|
||||
const hash = crypto.createHash('sha256');
|
||||
hash.update(fileDetails.join('|'));
|
||||
hash.update(fileDetails.join('\x00'));
|
||||
|
||||
return hash.digest('hex');
|
||||
}
|
@ -1,30 +1,37 @@
|
||||
import { getCurrentFormattedDateTime } from '$lib/songUpdateTime';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export async function GET({ params }) {
|
||||
const filePath = `./data/song/${params.id}.json`;
|
||||
if (!fs.existsSync(filePath)) {
|
||||
const filePath = path.join('./data/song', `${params.id}.json`);
|
||||
let data;
|
||||
try { data = fs.readFileSync(filePath); } catch (e) {
|
||||
return error(404, {
|
||||
message: "No correspoding song."
|
||||
})
|
||||
});
|
||||
}
|
||||
const data = fs.readFileSync(filePath);
|
||||
return json(JSON.parse(data.toString()));
|
||||
return json(JSON.parse(data.toString()));
|
||||
}
|
||||
|
||||
export async function POST({ params, request }) {
|
||||
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
|
||||
});
|
||||
}
|
@ -1,22 +1,17 @@
|
||||
/** @type {import('./$types').PageLoad} */
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
|
||||
export function load({ params }) {
|
||||
const filePath = `./data/song/${params.id}.json`;
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return {
|
||||
songData: null
|
||||
}
|
||||
}
|
||||
const dataBuffer = fs.readFileSync(filePath);
|
||||
const filePath = path.join('./data/song', `${params.id}.json`);
|
||||
try {
|
||||
const dataBuffer = fs.readFileSync(filePath);
|
||||
const data = JSON.parse(dataBuffer.toString());
|
||||
return {
|
||||
songData: data
|
||||
};
|
||||
}
|
||||
catch {
|
||||
} catch {
|
||||
return {
|
||||
songData: null
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user