File size: 4,008 Bytes
183a3de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import path from "path";
import fs from "fs/promises";
import schedule from "node-schedule";
import chalk from "chalk";
import { checkUrlStatus } from "./util.js";

const CACHE_FILE_PATH = path.join(process.cwd(), "assets", "json-data", "cache-link.json");

export const cacheState = {
	data: {
		youtube: {},
		tiktok: {},
		zingmp3: {},
		soundcloud: {},
		nhaccuatui: {},
	},
	hasChanges: false,
};

async function loadCache() {
	try {
		const data = await fs.readFile(CACHE_FILE_PATH, "utf8");
		cacheState.data = JSON.parse(data);
		console.log(chalk.magentaBright("Đã nạp dữ liệu cache từ file thành công"));
	} catch (error) {
		console.error("Lỗi khi đọc file cache:", error);
	}
}

async function saveCache() {
	try {
		await fs.writeFile(CACHE_FILE_PATH, JSON.stringify(cacheState.data, null, 2));
	} catch (error) {
		console.error("Lỗi khi ghi file cache:", error);
	}
}

async function checkAndSaveChanges() {
	if (cacheState.hasChanges) {
		await saveCache();
		cacheState.hasChanges = false;
	}
}

export async function initializeCacheService() {
	await loadCache();

	schedule.scheduleJob("*/5 * * * * *", async () => {
		await checkAndSaveChanges();
	});

	console.log(chalk.magentaBright("Khởi động service quản lý cache link platform hoàn tất"));
}

function getCacheData(platform, id) {
	if (!cacheState.data[platform]) return null;

	const cache = cacheState.data[platform][id];
	if (!cache) return null;
	return cache;
}

export async function getCachedMedia(platform, id, quality = null, title = null) {
	const isValid = await validateCache(platform, id, quality);
	if (!isValid) return null;

	const cachedData = getCacheData(platform, id);
	if (!cachedData) return null;
	if (title && (!cachedData.title || cachedData.title !== title)) {
		cachedData.title = title;
		cacheState.hasChanges = true;
	}
	const linkChoose = quality ? cachedData[quality] : cachedData;

	return {
		fileUrl: linkChoose.fileUrl,
		title: linkChoose.title || cachedData.title,
		platform: linkChoose.platform || platform,
		width: linkChoose.width || cachedData.width,
		height: linkChoose.height || cachedData.height,
		duration: linkChoose.duration || cachedData.duration,
	};
}

export function setCacheData(platform, id, data, quality = null) {
	if (!cacheState.data[platform]) cacheState.data[platform] = {};

	if (!cacheState.data[platform][id]) cacheState.data[platform][id] = {};

	if (data.title) {
		if (!cacheState.data[platform][id].title || cacheState.data[platform][id].title !== data.title) {
			cacheState.data[platform][id].title = data.title;
		}
		delete data.title;
	}

	if (data.artist) {
		if (!cacheState.data[platform][id].artist || cacheState.data[platform][id].artist !== data.artist) {
			cacheState.data[platform][id].artist = data.artist;
		}
		delete data.artist;
	}

	if (quality) {
		cacheState.data[platform][id][quality] = {
			...data,
			timestamp: Date.now(),
		};
	} else {
		cacheState.data[platform][id] = {
			...cacheState.data[platform][id],
			...data,
			timestamp: Date.now(),
		};
	}

	cacheState.hasChanges = true;
}

function deleteCacheData(platform, id, quality = null) {
	if (!cacheState.data[platform]?.[id]) return;

	if (quality) {
		delete cacheState.data[platform][id][quality];
		if (Object.keys(cacheState.data[platform][id]).length === 0) {
			delete cacheState.data[platform][id];
		}
	} else {
		delete cacheState.data[platform][id];
	}

	cacheState.hasChanges = true;
}

async function validateCache(platform, id, quality = null) {
	const cacheEntry = getCacheData(platform, id);
	if (!cacheEntry) return false;

	const linkChoose = quality ? cacheEntry[quality] : cacheEntry;
	if (!linkChoose) return false;

	const isValid = await checkUrlStatus(linkChoose.fileUrl);
	if (!isValid) {
		deleteCacheData(platform, id, quality);
		return false;
	}

	return true;
}