-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
146 lines (123 loc) · 4.5 KB
/
index.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
import { SEAL, mediaAsset } from './dist/seal.js'
import { detectMimeType } from "./dist/mimetypes.js"
const textDecoder = new TextDecoder();
let status_element = document.getElementById("console");
let result_element = document.getElementById("result");
let result_logo = document.getElementById("result_logo");
let fileUpload = document.getElementById("upload");
window.decode_ex = (image) => {
display_img.src = image.src
imageSrcToFile(image.src, image.name).then(file => {
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = async () => {
file.array_buffer = reader.result
file.format = file.type;
fileRead(file);
}
});
}
media_container.addEventListener("dragover", (event) => {
event.preventDefault();
media_container.style.backgroundColor = "#e5e7eb";
});
media_container.addEventListener("drop", async (e) => {
e.preventDefault();
if (e.dataTransfer.files.length) {
let file = e.dataTransfer.files[0];
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = async () => {
file.array_buffer = reader.result
file.format = file.type;
fileRead(file);
}
}
})
fileUpload.addEventListener("change", function (b) {
let file = b.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = async () => {
file.array_buffer = reader.result
file.format = file.type;
fileRead(file);
}
});
async function fileRead(file) {
media_container.style.backgroundColor = "unset";
tooltip.style.display = "none";
result_element.style.display = "none";
status_element.innerHTML = hljs.highlight(
'Reading file...',
{ language: 'bash' }
).value
display_text.innerHTML = '';
display_text.style.display = "none"
display_img.src = '';
display_img.style.display = "none"
display_video.src = ''
display_video.removeAttribute('src')
display_video.style.display = "none"
if (file.format.length === 0) {
file.format = detectMimeType(file.array_buffer)
}
document.title = file.name + " (" + file.format + ")";
if (file.format.includes("image")) {
display_img.src = URL.createObjectURL(file);
display_img.style.display = "block"
} else if (file.format.includes("audio") || file.format.includes("video")) {
display_video.src = URL.createObjectURL(file)
display_video.style.display = "block"
} else if (file.format.includes("text")) {
//TODO, do we really want arbitrary text to be injected into the page?
display_text.innerHTML = "<pre>" + textDecoder.decode(file.array_buffer) + "</pre>";
display_text.style.display = "block"
}
console.log(`********[${file.name}] (${file.format})*******`);
let asset = new mediaAsset(file.array_buffer, file.name);
//asset.dumpInfo();
if (asset.seal_segments.length > 0) {
try {
SEAL.parse(asset);
let summary = await SEAL.validateSig(asset,true)
status_element.innerHTML = hljs.highlight(summary.summary, { language: 'console' }).value
if (summary.result == true) {
result_element.style.display = "unset";
result_logo.src = "./static/valid.svg";
} else {
result_element.style.display = "unset";
result_logo.src = "./static/unvalid.svg";
}
} catch (error) {
console.error(error)
status_element.innerHTML = hljs.highlight(
JSON.stringify(error) + "\n" + error.message,
{ language: 'bash' }
).value
}
} else {
status_element.innerHTML = hljs.highlight(
"😢 No SEAL signatures found.",
{ language: 'bash' }
).value
}
}
async function imageSrcToFile(imageSrc, fileName) {
try {
// Fetch the image
const response = await fetch(imageSrc);
// Ensure the fetch was successful
if (!response.ok) {
throw new Error('Failed to fetch image');
}
// Get the image as a Blob
const blob = await response.blob();
// Create a File from the Blob
const file = new File([blob], fileName, { type: blob.type });
return file;
} catch (error) {
console.error('Error converting image src to File:', error);
}
}