-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
117 lines (100 loc) · 3.87 KB
/
app.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
this.sideBarEdit("File Import & Export", [{
label: "Import JSON",
button: true,
id: "import-file-json",
icon: `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="w-4 h-4 -mt-[1px]"><path d="M4 22h14a2 2 0 0 0 2-2V7l-5-5H6a2 2 0 0 0-2 2v4"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M4 12a1 1 0 0 0-1 1v1a1 1 0 0 1-1 1 1 1 0 0 1 1 1v1a1 1 0 0 0 1 1"/><path d="M8 18a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1 1 1 0 0 1-1-1v-1a1 1 0 0 0-1-1"/></svg>`,
}]);
fileDropdownLinks([
{
label: "Export as JSON",
button: true,
dataId: "export-as-json",
multi: false,
onclick: exportAsJSON
}
]);
async function exportAsJSON(file) {
if (file.id) {
const response = await fetch(`/api/file/${file.id}`);
const fileData = await response.json();
const jsonString = JSON.stringify(fileData, null, 4);
const blob = new Blob([jsonString], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = fileData.Filename.replace(/\.[^/.]+$/, ".json");
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
showAlert(`Downloaded '${fileData.Filename}' as a raw JSON file`, "success");
return;
}
}
document.querySelector('#import-file-json').addEventListener("click", () => {
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.style.display = "none";
document.body.appendChild(fileInput);
fileInput.addEventListener('change', function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = async function(e) {
try {
var json = JSON.parse(e.target.result);
}
catch {
var json = false;
}
function convertKeysToUpperCase(obj) {
if (typeof obj !== 'object' || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(convertKeysToUpperCase);
}
return Object.keys(obj).reduce((acc, key) => {
let newKey;
if (key === 'uploaded_size') {
newKey = 'UploadedSize';
} else if (key === 'url' || key === 'size') {
newKey = key;
} else {
newKey = key.charAt(0).toUpperCase() + key.slice(1);
}
acc[newKey] = convertKeysToUpperCase(obj[key]);
return acc;
}, {});
}
if (json) {
json = convertKeysToUpperCase(json);
json.Timestamp = null;
json.UserId = null;
json.GlobalExpiry = 0;
showAlert(`Importing '${json.Filename}'`, "success");
const response = await fetch(`/api/create/files`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(json)
})
var data = await response.json();
if (response.status === 200) {
showAlert(`Imported '${json.Filename}'`, "success");
pushPath("/dashboard/files");
}
else {
showAlert(data.error || "Unknown Error", "error");
}
}
else {
showAlert("JSON is not valid!", "error");
}
};
reader.readAsText(file);
}
});
fileInput.click();
});