-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentful_script.js
149 lines (130 loc) · 3.83 KB
/
contentful_script.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
148
149
var lang = 'en-US'
var accessToken = 'CFPAT-9b7a21488af237b472d49f2a7a235f8dacc4f99cee5df788bcb47bf6c5a4bb4d'
var space_id = 'vxu5be6w735m'
var query = {}
query.content_type = 'ashTree'
var baseAPI = 'https://api.contentful.com'
var assetAPI = baseAPI +"/spaces/" + space_id + "/assets/<asset_id>"; // ?access_token=" + accessToken;
var client = contentful.createClient({
// This is the access token for this space. Normally you get the token in the Contentful web app
accessToken: accessToken
})
function assetEndpoint(asset_id) {
return assetAPI.replace("<asset_id>", asset_id);
}
function getPreciseLocation() {
return new Promise(function (resolve, reject) {
navigator.geolocation.getCurrentPosition(function (position) {
resolve([position.coords.latitude, position.coords.longitude]);
});
});
}
function createAshTreeImages(entry) {
client.getSpace(space_id)
.then((space) => space.createEntry('ashTreeImages', {
fields: {
s3url: {
'en-US': 'https://s3.amazonaws.com/ash-tree-photos/' + img_name
},
imageType: {
'en-US': 'Tree'
},
ashTree: {
'en-US': {
sys: {
type: 'Link',
linkType: 'Entry',
id: entry.sys.id
}
}
}
}
}))
.then((entry) => console.log(entry))
.catch(console.error)
}
function done(entry){
window.location.replace('./uploadsuccess.html')
}
function createAshTree(data) {
var d = new Date();
var n = d.toISOString();
client.getSpace(space_id)
.then((space) => space.createEntry('ashTree', {
fields: {
latlong: {
'en-US': {
lat: data[0],
lon: data[1]
}
},
datetime: {
'en-US': n
},
s3url: {
'en-US': 'https://s3.amazonaws.com/ash-tree-photos/' + img_name
}
}
}))
.then((entry) => done(entry))//refresh())
.catch(console.error)
}
var input = document.querySelector("input[type=file]");
var result_image_obj = '';
var img_name;
input.addEventListener("change", function (e) {
img_name = input.files[0].name;
var label = $(e.target).prev();
label.css("border", "green solid 2px");
createImageBitmap(input.files[0])
.then(response => {
compress(response);
});
});
function compress(source_img_obj) {
var cvs = document.createElement("canvas");
cvs.width = source_img_obj.width;
cvs.height = source_img_obj.height;
var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0);
var newImageData = cvs.toDataURL("image/jpeg", 0.5);
result_image_obj = newImageData;
}
//Add Photo
$(document).on("click", "#upload", function(){
// Update entry
var url = "https://s3.amazonaws.com/ash-tree-photos";
var base64ImageContent = result_image_obj.replace(/^data:image\/(png|jpeg);base64,/, "");
var blob = base64ToBlob(base64ImageContent, 'image/jpeg');
var blobFile = new File([blob], img_name);
var formData = new FormData();
formData.append('key', blobFile.name);
formData.append('file', blobFile);
$.ajax({
url: url,
type: "POST",
cache: false,
contentType: false,
processData: false,
data: formData
}).done(function(e){
getPreciseLocation()
.then(createAshTree);
});
});
function base64ToBlob(base64, mime)
{
mime = mime || '';
var sliceSize = 1024;
var byteChars = window.atob(base64);
var byteArrays = [];
for (var offset = 0, len = byteChars.length; offset < len; offset += sliceSize) {
var slice = byteChars.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new Blob(byteArrays, {type: mime});
}