update: logic of loading data
This commit is contained in:
parent
815544ea4e
commit
e548326977
@ -1,33 +1,39 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { globalMemoryStorage, songData, songNameCache } from '$lib/server/cache.js';
|
import { globalMemoryStorage, songData, songNameCache } from '$lib/server/cache.js';
|
||||||
|
import { getDirectoryHash } from '../dirHash';
|
||||||
|
|
||||||
|
const dataPath = './data/song/';
|
||||||
|
|
||||||
export async function loadData() {
|
export async function loadData() {
|
||||||
const LastLoaded: number | undefined = globalMemoryStorage.get("lastLoadData");
|
const LastLoaded: number | undefined = globalMemoryStorage.get('lastLoadData');
|
||||||
|
const LastHash: string | undefined = globalMemoryStorage.get('lastDataDirHash');
|
||||||
const currentTime = new Date().getTime();
|
const currentTime = new Date().getTime();
|
||||||
|
const currentHash = getDirectoryHash(dataPath);
|
||||||
// Already loaded.
|
// Already loaded.
|
||||||
if (LastLoaded && currentTime - LastLoaded < 3600) {
|
if (LastLoaded && LastHash && currentTime - LastLoaded < 120 * 1000 && currentHash === LastHash) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const dataPath = "./data/song/";
|
const songList = fs
|
||||||
const songList = fs.readdirSync(dataPath)
|
.readdirSync(dataPath)
|
||||||
.map(fileName => {
|
.map((fileName) => {
|
||||||
if (fileName.endsWith(".json"))
|
if (fileName.endsWith('.json')) return fileName.slice(0, fileName.length - 5);
|
||||||
return fileName.slice(0, fileName.length - 5);
|
|
||||||
else return null;
|
else return null;
|
||||||
})
|
})
|
||||||
.filter(fileName => fileName !== null);
|
.filter((fileName) => fileName !== null);
|
||||||
|
songData.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(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;
|
||||||
songNameCache.set(metadata.name, metadata);
|
songNameCache.set(metadata.name, metadata);
|
||||||
}
|
} catch {
|
||||||
catch {
|
|
||||||
console.error(`[load-song-data] Could not load song ID ${songID}`);
|
console.error(`[load-song-data] Could not load song ID ${songID}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
globalMemoryStorage.set("lastLoadData", new Date().getTime());
|
globalMemoryStorage.set('lastLoadData', new Date().getTime());
|
||||||
}
|
globalMemoryStorage.set('lastDataDirHash', getDirectoryHash(dataPath));
|
||||||
|
}
|
||||||
|
36
src/lib/server/dirHash.ts
Normal file
36
src/lib/server/dirHash.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
import * as crypto from 'crypto';
|
||||||
|
|
||||||
|
// Function to get hash for a given directory
|
||||||
|
export function getDirectoryHash(dir: string): string {
|
||||||
|
const fileDetails: string[] = [];
|
||||||
|
|
||||||
|
// Recursive function to traverse the directory
|
||||||
|
function traverseDirectory(currentDir: string): void {
|
||||||
|
const files = fs.readdirSync(currentDir);
|
||||||
|
|
||||||
|
files.forEach(file => {
|
||||||
|
const filePath = path.join(currentDir, file);
|
||||||
|
const stats = fs.statSync(filePath);
|
||||||
|
|
||||||
|
if (stats.isDirectory()) {
|
||||||
|
traverseDirectory(filePath);
|
||||||
|
} else {
|
||||||
|
// Collect file path and last modification time
|
||||||
|
fileDetails.push(`${filePath}:${stats.mtimeMs}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
traverseDirectory(dir);
|
||||||
|
|
||||||
|
// Sort file details to ensure consistent hash
|
||||||
|
fileDetails.sort();
|
||||||
|
|
||||||
|
// Create hash from file details
|
||||||
|
const hash = crypto.createHash('sha256');
|
||||||
|
hash.update(fileDetails.join('|'));
|
||||||
|
|
||||||
|
return hash.digest('hex');
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user