-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload.js
91 lines (75 loc) · 2.12 KB
/
upload.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
//TODO: Create json hash as a global variable that can be changed
//TODO: On file upload, update the new JSONDB hash
var ipfs = window.IpfsApi('ipfs.infura.io', '5001', {protocol: 'https'});
const Buffer = window.IpfsApi().Buffer;
function validateFormOnSubmit(form) {
var reason = "";
reason += validateAudio(form.audio.value);
reason += validateName(form.name.value);
reason += validateArtist(form.artist.value);
//Empty Fields
if (reason != "") {
alert("Some fields need correction:\n" + reason);
}
//All Fields are Input
else {
var reader = getReader();
//TODO: Any string can be converted into a compatible Buffer, but a Binary String Cannot be converted
// ipfs.add(Buffer.from("String"), ...) works
reader.onloadend = e => {
//Get Raw Data
var rawData = reader.result;
//Add File to IPFS
ipfs.add(Buffer.from(rawData), (err, result) => {
if (err || !result) {
alert(`Unable to upload to IPFS API: ${err}`);
} else {
resultHash = result[0].hash;
// console.log(resultHash);
alert("Success! Hash: " + resultHash);
}
})
//Add Upload hash to JSON DB
// #TODO:
}
}
return false;
}
function getReader() {
var file = document.getElementById('fileuploader');
if(file.files.length) {
var reader = new FileReader();
reader.readAsArrayBuffer(file.files[0]);
/*
reader.onload = function(e) {
var rawData = reader.result;
console.log(rawData);
return rawData;
};
*/
return reader
//reader.readAsBinaryString(file.files[0]);
}
}
function validateName(name) {
if (!name) {
return "Name ";
} else {
return "";
}
}
function validateArtist(artist) {
if (!artist) {
return "Artist ";
} else {
return "";
}
}
function validateAudio(file) {
if (!file) {
return "Audio File ";
} else {
console.log(file);
return "";
}
}