-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbScript.js
133 lines (103 loc) · 4.24 KB
/
dbScript.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
let indexDbObj = indexedDB.open("Camera", 1);
let db;
indexDbObj.addEventListener("success", function(){
db = indexDbObj.result; // access it if database exits
})
indexDbObj.addEventListener("error", function(){
console.log("error");
})
indexDbObj.addEventListener("upgradeneeded", function(){
db = indexDbObj.result; // creates database, if not exists
// creating table
db.createObjectStore("gallery", {
keyPath: "mId"
})
})
function addMediaToGallery(data, type){
console.log("Hello")
let txAccess = db.transaction("gallery", "readwrite");
let galleryStore = txAccess.objectStore("gallery");
let galleryEntry = {
mId: Date.now(),
type,
media: data
}
galleryStore.add(galleryEntry);
}
function viewMedia(){
let mediaContainer = document.querySelector(".media-container");
let txAccess = db.transaction("gallery", "readonly");
let galleryStore = txAccess.objectStore("gallery");
let request = galleryStore.openCursor()
request.addEventListener("success", function(){
let cursor = request.result;
if (cursor) {
if(cursor.value.type == "video"){
let videoContainer = document.createElement("div");
videoContainer.setAttribute("data-mId", cursor.value.mId);
videoContainer.classList.add("gallery_video-container");
let vidElem = document.createElement("video");
vidElem.autoplay = true;
vidElem.controls = true;
vidElem.loop = true;
// vidElem.srcObject = cursor.value.media;
vidElem.src = window.URL.createObjectURL(cursor.value.media);
videoContainer.appendChild(vidElem);
let deleteBtn = document.createElement("button");
deleteBtn.classList.add("gallery_delete-btn");
deleteBtn.innerText = "Delete"
deleteBtn.addEventListener("click", handleDelete);
let downloadBtn = document.createElement("button");
downloadBtn.classList.add("gallery_download-btn");
downloadBtn.innerText = "Download";
downloadBtn.addEventListener("click", handleDownload);
videoContainer.appendChild(deleteBtn);
videoContainer.appendChild(downloadBtn);
mediaContainer.appendChild(videoContainer);
}else{
let imageContainer = document.createElement("div");
imageContainer.setAttribute("data-mId", cursor.value.mId);
imageContainer.classList.add("gallery_img-container");
let image = document.createElement("img");
image.src = cursor.value.media;
imageContainer.appendChild(image);
let deleteBtn = document.createElement("button");
deleteBtn.classList.add("gallery_delete-btn");
deleteBtn.innerText = "Delete"
deleteBtn.addEventListener("click", handleDelete);
let downloadBtn = document.createElement("button");
downloadBtn.classList.add("gallery_download-btn");
downloadBtn.innerText = "Download";
downloadBtn.addEventListener("click", handleDownload);
imageContainer.appendChild(deleteBtn);
imageContainer.appendChild(downloadBtn);
mediaContainer.appendChild(imageContainer);
}
cursor.continue();
} else {
console.log("No more data");
}
})
}
function deleteMediaFromGallery(id){
let txAccess = db.transaction("gallery", "readwrite");
let galleryStore = txAccess.objectStore("gallery");
galleryStore.delete(Number(id));
}
function handleDelete(e){
let mId = e.currentTarget.parentNode.getAttribute("data-mId");
deleteMediaFromGallery(mId);
e.currentTarget.parentNode.remove();
}
function handleDownload(e){
let url = e.currentTarget.parentNode.children[0].src;
let a = document.createElement("a");
a.href = url;
if(e.currentTarget.parentNode.children[0].nodeName == "IMG"){
a.download = "image.png";
}else{
a.download = "video.mp4";
}
a.click();
a.remove();
}