-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgallery.js
147 lines (123 loc) · 4.88 KB
/
gallery.js
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
setTimeout(() => {
if (db) {
// videos retrieval
const videoTransaction = db.transaction("video", "readonly");
const videoStore = videoTransaction.objectStore("video");
const videoRequest = videoStore.getAll();
videoRequest.addEventListener("success", (event) => {
let videos = videoRequest.result;
let displayGallery = document.querySelector(".display-gallery");
videos.forEach((video) => {
let mediaGallery = document.createElement("div");
mediaGallery.setAttribute("class", "media-gallery");
mediaGallery.setAttribute("id", video.id);
let url = URL.createObjectURL(video.blobData);
mediaGallery.innerHTML = `
<div class="media">
<video src=${url} autoplay loop></video>
</div>
<div class="media-actions">
<div class="download">
<img src="./icons/download.png" alt="Download Media" />
</div>
<div class="delete">
<img src="./icons/delete.png" alt="Delete Media" />
</div>
</div>
`;
displayGallery.appendChild(mediaGallery);
let deleteAction = mediaGallery.querySelector(".delete");
deleteAction.addEventListener("click", deleteMedia);
let downloadAction = mediaGallery.querySelector(".download");
downloadAction.addEventListener("click", downloadMedia);
});
});
// images retrieval
const imageTransaction = db.transaction("image", "readonly");
const imageStore = imageTransaction.objectStore("image");
const imageRequest = imageStore.getAll();
imageRequest.addEventListener("success", (event) => {
let images = imageRequest.result;
let displayGallery = document.querySelector(".display-gallery");
images.forEach((image) => {
let mediaGallery = document.createElement("div");
mediaGallery.setAttribute("class", "media-gallery");
mediaGallery.setAttribute("id", image.id);
mediaGallery.innerHTML = `
<div class="media">
<img src=${image.url} alt=${image.id} />
</div>
<div class="media-actions">
<div class="download">
<img src="./icons/download.png" alt="Download Media" />
</div>
<div class="delete">
<img src="./icons/delete.png" alt="Delete Media" />
</div>
</div>
`;
displayGallery.appendChild(mediaGallery);
let deleteAction = mediaGallery.querySelector(".delete");
deleteAction.addEventListener("click", deleteMedia);
let downloadAction = mediaGallery.querySelector(".download");
downloadAction.addEventListener("click", downloadMedia);
});
});
}
}, 100);
function deleteMedia(event) {
const mediaGallery = event.target.parentNode.parentNode.parentNode;
const id = mediaGallery.getAttribute("id");
const type = id.slice(0, 5);
if (type === "video") {
let videoDBTransaction = db.transaction("video", "readwrite");
let videoStore = videoDBTransaction.objectStore("video");
videoStore.delete(id);
} else if (type === "image") {
let imageDBTransaction = db.transaction("image", "readwrite");
let imageStore = imageDBTransaction.objectStore("image");
imageStore.delete(id);
}
mediaGallery.remove();
}
function downloadMedia(event) {
const mediaGallery = event.target.parentNode.parentNode.parentNode;
const id = mediaGallery.getAttribute("id");
const type = id.slice(0, 5);
if (type === "video") {
const videoTransaction = db.transaction("video", "readonly");
const videoStore = videoTransaction.objectStore("video");
const videoRequest = videoStore.get(id);
videoRequest.addEventListener("success", () => {
let video = videoRequest.result;
const videoURL = URL.createObjectURL(video.blobData);
const a = document.createElement("a");
a.href = videoURL;
const formattedDate = getFormattedDate();
a.download = `stream ${formattedDate} by captura.mp4`;
a.click();
});
} else if (type === "image") {
const imageTransaction = db.transaction("image", "readonly");
const imageStore = imageTransaction.objectStore("image");
let imageRequest = imageStore.get(id);
imageRequest.addEventListener("success", () => {
let image = imageRequest.result;
let a = document.createElement("a");
a.href = image.url;
const formattedDate = getFormattedDate();
a.download = `image ${formattedDate}.jpg`;
a.click();
});
}
}
function getFormattedDate() {
const date = new Date();
const formattedDate = date.toLocaleDateString();
const formattedTime = date.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
return `${formattedDate} at ${formattedTime}`;
}