-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.js
More file actions
52 lines (42 loc) · 1.63 KB
/
Copy pathtest2.js
File metadata and controls
52 lines (42 loc) · 1.63 KB
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
const { initializeApp, cert } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
const fs = require('fs');
const serviceAccount = JSON.parse(fs.readFileSync('./functions/serviceAccountKey.json', 'utf8'));
initializeApp({ credential: cert(serviceAccount) });
const db = getFirestore();
async function test() {
const tracksSnap = await db.collection("tracks").get();
const tracks = tracksSnap.docs.map(doc => doc.data());
const usersSnap = await db.collection("users").get();
const users = new Map();
usersSnap.forEach(doc => users.set(doc.id, doc.data()));
const getKey = (url) => {
if (!url) return "";
try {
return decodeURIComponent(new URL(url).pathname.replace(/^\//, ''));
} catch {
return decodeURIComponent(url.replace(/^https?:\/\/[^\/]+\//, ''));
}
};
const ownersMap = {};
tracks.forEach(track => {
const ownerInfo = {
name: users.get(track.userId)?.displayName || "Inconnu",
id: track.userId
};
if (track.url) ownersMap[getKey(track.url)] = ownerInfo;
if (track.coverUrl) ownersMap[getKey(track.coverUrl)] = ownerInfo;
});
console.log("Found " + Object.keys(ownersMap).length + " mapped files from tracks.");
console.log("Mapped Keys Example:", Object.keys(ownersMap).slice(0, 3));
console.log("-------------------");
// Let's pretend R2 returns these keys:
const dummyR2Keys = [
"uploads/1783554519480-Grey afternoonBOUNCES.wav",
"uploads/1786207189317-poutchi v1.mp3"
];
dummyR2Keys.forEach(key => {
console.log("Match for " + key + " :", ownersMap[key]);
});
}
test().catch(console.error);