-
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.
- Loading branch information
Showing
2 changed files
with
55 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,39 @@ | ||
import fs from 'fs'; | ||
import { globalMemoryStorage, songData, songNameCache } from '$lib/server/cache.js'; | ||
import { getDirectoryHash } from '../dirHash'; | ||
|
||
const dataPath = './data/song/'; | ||
|
||
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 currentHash = getDirectoryHash(dataPath); | ||
// Already loaded. | ||
if (LastLoaded && currentTime - LastLoaded < 3600) { | ||
if (LastLoaded && LastHash && currentTime - LastLoaded < 120 * 1000 && currentHash === LastHash) { | ||
return; | ||
} | ||
|
||
const dataPath = "./data/song/"; | ||
const songList = fs.readdirSync(dataPath) | ||
.map(fileName => { | ||
if (fileName.endsWith(".json")) | ||
return fileName.slice(0, fileName.length - 5); | ||
const songList = fs | ||
.readdirSync(dataPath) | ||
.map((fileName) => { | ||
if (fileName.endsWith('.json')) return fileName.slice(0, fileName.length - 5); | ||
else return null; | ||
}) | ||
.filter(fileName => fileName !== null); | ||
.filter((fileName) => fileName !== null); | ||
songData.flushAll(); | ||
songNameCache.flushAll(); | ||
for (const songID of songList) { | ||
try { | ||
const fileContentString = fs.readFileSync(dataPath + songID + ".json").toString(); | ||
const fileContentString = fs.readFileSync(dataPath + songID + '.json').toString(); | ||
const data = JSON.parse(fileContentString); | ||
songData.set(songID, data); | ||
const metadata: MusicMetadata = data; | ||
songNameCache.set(metadata.name, metadata); | ||
} | ||
catch { | ||
} catch { | ||
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)); | ||
} |
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,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'); | ||
} |