-
Notifications
You must be signed in to change notification settings - Fork 1
/
firebase.mjs
146 lines (131 loc) · 4.44 KB
/
firebase.mjs
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
import { initializeApp } from "firebase/app";
import { getStorage, ref as storage_ref, getDownloadURL } from "firebase/storage";
import { getDatabase, ref as database_ref, get, query as database_query, orderByChild, limitToLast, startAt, endBefore, limitToFirst, child, startAfter } from "firebase/database";
import { getFirestore, collection, query as firestore_query, where, getDocs, doc, getDoc } from "firebase/firestore";
const firebaseConfig = {
apiKey: "AIzaSyCQ-5t6Uzc9SvuvamD3wiEoHhiY_zMMxrM",
authDomain: "origin-performing-art.firebaseapp.com",
databaseURL: "https://origin-performing-art-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "origin-performing-art",
storageBucket: "origin-performing-art.appspot.com",
messagingSenderId: "32164182626",
appId: "1:32164182626:web:f98d68695e8b02c0542f3b",
measurementId: "G-5BMJEJHT05"
};
const app = initializeApp(firebaseConfig);
async function fetchFileURL(storage, path) {
return await getDownloadURL(storage_ref(storage, path));
}
async function fetchImage(url) {
const storage = getStorage(app);
return await fetchFileURL(storage, url);
}
async function fetchBlog({ id }) {
try {
const storage = getStorage(app);
const db = getFirestore(app);
const docSnap = await getDoc(doc(db, 'blog', id));
if (docSnap.exists()) {
var blog = docSnap.data();
blog.image = await fetchFileURL(storage, blog.image);
// blog.html = blog.html.replaceAll('\\n', '\n');
blog.code = 200;
return blog;
}
} catch (e) {
return {
error: e,
code: 500
};
}
return {
code: 404,
error: 'No such document!'
};
}
async function fetchAllData(type) {
try {
const db = getFirestore(app);
const querySnapshot = await getDocs(collection(db, type));
var data = {};
await querySnapshot.forEach((doc) => {
data[doc.id] = doc.data();
});
return data;
} catch (e) {
console.error(e)
return {
error: e,
code: 500
};
}
return {
code: 404,
error: 'No such document!'
};
}
async function fetchPostList({ before, type }) {
try {
var list = [];
const database = getDatabase(app);
const storage = getStorage(app);
var order = database_query(database_ref(database, type + 's'), orderByChild('sort'));
var filter = database_query(order, endBefore(before), limitToLast(10));
const data = await get(filter).then(async snapshot => await snapshot.val());
const keys = Object.keys(data);
console.log(data)
for (let i = 0; i < keys.length; i++) {
if (typeof data[keys[i]] != 'object') continue; // 避免處理maxSort
var d = data[keys[i]];
d.oid = keys[i];
d.image = await fetchFileURL(storage, d.image);
list.push(d);
}
list.sort((a, b) => b.sort - a.sort);
} catch (e) {
console.log(e)
}
return list;
}
async function fetchEvent({ id }) {
try {
const storage = getStorage(app);
const db = getFirestore(app);
const docSnap = await getDoc(doc(db, 'event', id));
if (docSnap.exists()) {
var event = docSnap.data();
event.image = await fetchFileURL(storage, event.image);
// event.html = event.html.replaceAll('\\n', '\n');
// event.lists = event.lists.map(list => {
// var start = list.start.toDate();
// var end = list.end.toDate();
// list.timeString = `${start.getMonth() + 1}/${start.getDate()} - ${end.getMonth() + 1}/${end.getDate()}, ${start.getFullYear()} ${start.getHours().toString().padStart(2, '0')}:${start.getMinutes().toString().padStart(2, '0')} - ${end.getHours().toString().padStart(2, '0')}:${end.getMinutes().toString().padStart(2, '0')}`;
// return list;
// })
event.code = 200;
return event;
}
} catch (e) {
return {
error: e,
code: 500
};
}
return {
code: 404,
error: 'No such document!'
};
}
async function fetchDatabase(path) {
const database = getDatabase(app);
return await get(child(database_ref(database), path)).then(snapshot => snapshot.val());
}
// async function fetchStorageByPath(path) {
// const storage = getStorage(app);
// return await fetchFileURL(storage, path);
// }
async function fetchStorageMultipleByPaths(paths) {
const storage = getStorage(app);
return await Promise.all(paths.map(async path => await fetchFileURL(storage, path)));
}
export { fetchImage, fetchBlog, fetchPostList, fetchEvent, fetchDatabase, fetchStorageMultipleByPaths, fetchAllData };