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 fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
import { globalMemoryStorage, songData, songNameCache } from '$lib/server/cache.js';
|
import { globalMemoryStorage, songData, songNameCache } from '$lib/server/cache.js';
|
||||||
import { getDirectoryHash } from '../dirHash';
|
import { getDirectoryHash } from '../dirHash';
|
||||||
|
|
||||||
@ -25,7 +26,7 @@ export async function loadData() {
|
|||||||
songNameCache.flushAll();
|
songNameCache.flushAll();
|
||||||
for (const songID of songList) {
|
for (const songID of songList) {
|
||||||
try {
|
try {
|
||||||
const fileContentString = fs.readFileSync(dataPath + songID + '.json').toString();
|
const fileContentString = fs.readFileSync(path.join(dataPath, songID + '.json')).toString();
|
||||||
const data = JSON.parse(fileContentString);
|
const data = JSON.parse(fileContentString);
|
||||||
songData.set(songID, data);
|
songData.set(songID, data);
|
||||||
const metadata: MusicMetadata = data;
|
const metadata: MusicMetadata = data;
|
||||||
|
@ -12,7 +12,7 @@ export function getDirectoryHash(dir: string): string {
|
|||||||
|
|
||||||
files.forEach(file => {
|
files.forEach(file => {
|
||||||
const filePath = path.join(currentDir, file);
|
const filePath = path.join(currentDir, file);
|
||||||
const stats = fs.statSync(filePath);
|
const stats = fs.lstatSync(filePath);
|
||||||
|
|
||||||
if (stats.isDirectory()) {
|
if (stats.isDirectory()) {
|
||||||
traverseDirectory(filePath);
|
traverseDirectory(filePath);
|
||||||
@ -30,7 +30,7 @@ export function getDirectoryHash(dir: string): string {
|
|||||||
|
|
||||||
// Create hash from file details
|
// Create hash from file details
|
||||||
const hash = crypto.createHash('sha256');
|
const hash = crypto.createHash('sha256');
|
||||||
hash.update(fileDetails.join('|'));
|
hash.update(fileDetails.join('\x00'));
|
||||||
|
|
||||||
return hash.digest('hex');
|
return hash.digest('hex');
|
||||||
}
|
}
|
@ -1,30 +1,37 @@
|
|||||||
import { getCurrentFormattedDateTime } from '$lib/songUpdateTime';
|
import { getCurrentFormattedDateTime } from '$lib/songUpdateTime';
|
||||||
import { json, error } from '@sveltejs/kit';
|
import { json, error } from '@sveltejs/kit';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
export async function GET({ params }) {
|
export async function GET({ params }) {
|
||||||
const filePath = `./data/song/${params.id}.json`;
|
const filePath = path.join('./data/song', `${params.id}.json`);
|
||||||
if (!fs.existsSync(filePath)) {
|
let data;
|
||||||
|
try { data = fs.readFileSync(filePath); } catch (e) {
|
||||||
return error(404, {
|
return error(404, {
|
||||||
message: "No correspoding song."
|
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 }) {
|
export async function POST({ params, request }) {
|
||||||
const timeStamp = new Date().getTime();
|
const timeStamp = new Date().getTime();
|
||||||
|
try {
|
||||||
if (!fs.existsSync("./data/pending/")) {
|
if (!fs.existsSync("./data/pending/")) {
|
||||||
fs.mkdirSync("./data/pending");
|
fs.mkdirSync("./data/pending", { mode: 0o755 });
|
||||||
}
|
}
|
||||||
const filePath = `./data/pending/${params.id}-${timeStamp}.json`;
|
const filePath = `./data/pending/${params.id}-${timeStamp}.json`;
|
||||||
const data: MusicMetadata = await request.json();
|
const data: MusicMetadata = await request.json();
|
||||||
data.updateTime = getCurrentFormattedDateTime();
|
data.updateTime = getCurrentFormattedDateTime();
|
||||||
fs.writeFileSync(filePath, JSON.stringify(data, null, 4));
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 4), { mode: 0o644 });
|
||||||
return json({
|
return json({
|
||||||
"message": "successfully created"
|
"message": "successfully created"
|
||||||
}, {
|
}, {
|
||||||
status: 201
|
status: 201
|
||||||
});
|
});
|
||||||
|
} catch (e) {
|
||||||
|
return error(500, {
|
||||||
|
message: "Internal server error."
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,22 +1,17 @@
|
|||||||
/** @type {import('./$types').PageLoad} */
|
/** @type {import('./$types').PageLoad} */
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
|
||||||
export function load({ params }) {
|
export function load({ params }) {
|
||||||
const filePath = `./data/song/${params.id}.json`;
|
const filePath = path.join('./data/song', `${params.id}.json`);
|
||||||
if (!fs.existsSync(filePath)) {
|
|
||||||
return {
|
|
||||||
songData: null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const dataBuffer = fs.readFileSync(filePath);
|
|
||||||
try {
|
try {
|
||||||
|
const dataBuffer = fs.readFileSync(filePath);
|
||||||
const data = JSON.parse(dataBuffer.toString());
|
const data = JSON.parse(dataBuffer.toString());
|
||||||
return {
|
return {
|
||||||
songData: data
|
songData: data
|
||||||
};
|
};
|
||||||
}
|
} catch {
|
||||||
catch {
|
|
||||||
return {
|
return {
|
||||||
songData: null
|
songData: null
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user