-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjavascript.html
388 lines (313 loc) · 11.7 KB
/
javascript.html
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var gs = google.script.run, // Server function access
// Sketchfab API
sketchfabApiUrl = 'https://api.sketchfab.com/v2/models',
sketchfabModelUrl = 'https://sketchfab.com/models/',
token = '', // Keep it secret. Keep it safe.
// Spreadsheet and model data organization
headers = [ 'file', 'name', 'description', 'tags', 'private', 'password', 'license', 'category' ],
columnCount = headers.length,
modelFiles,
modelCount,
rowCount,
modelsArr,
modelsPatchArr = [],
lics = [
{
'uid': 1,
'slug': 'CC Attribution'
},
{
'uid': 2,
'slug': 'CC Attribution-ShareAlike'
},
{
'uid': 3,
'slug': 'CC Attribution-NoDerivs'
},
{
'uid': 4,
'slug': 'CC Attribution-NonCommercial'
},
{
'uid': 5,
'slug': 'CC Attribution-NonCommercial-ShareAlike'
},
{
'uid': 6,
'slug': 'CC Attribution-NonCommercial-NoDerivs'
},
{
'uid': 7,
'slug': 'CC0 Public Domain'
}
],
cats,
// Form DOM object
formData = $( '#the-form' )[ 0 ],
// Upload data
uploadIndex = 0,
dataArr = [],
// Buttons
uploadButton = $( '#upload' ),
clearButton = $( '#clear' ),
fileInput = $( '#modelFiles' );
// Button event handling
uploadButton.on( 'click', function() {
console.log( 'Upload button was clicked' );
gs.withSuccessHandler( prepareUpload )
.getMetaData();
});
clearButton.on( 'click', function() {
console.log( 'Clear button was clicked' );
uploadIndex = 0;
$( '#status, #fileCount' ).empty();
$( '#token, #modelFiles' ).val( '' );
gs.withSuccessHandler( sheetCleared )
.clearSheet();
});
fileInput.on( 'change', function() {
var fileCount = this.files.length,
fileText = ' files';
if ( fileCount === 1 ) {
fileText = ' file';
}
$( '#fileCount' ).empty()
.append( '<span>' + fileCount + fileText + '</span>' );
});
formData.onsubmit = function() {
// API token
token = formData[ 'token' ].value;
console.log( 'API Token: ' + token );
// Populate model list
modelFiles = formData[ 'modelFiles' ].files;
modelCount = modelFiles.length;
console.log( 'Model count: ' + modelCount );
rowCount = modelCount + 1;
// An array of empty strings for empty cells
var blanks = [];
for ( var i = 0; i < columnCount - 1; i++ ) {
blanks.push( '' );
}
// 2D array for sheet formatting
modelsArr = [ headers ];
for ( var i = 0; i < modelCount; i++ ) {
// Get model filename and add blank cells to row
var fileName = [ modelFiles[ i ].name ],
modelArr = fileName.concat( blanks );
console.log( fileName );
// Prepare DOM for status polling
$( '#status' ).append( '<p class="file-name">' + fileName + '</p><p class="file-status" id="model' + ( i + 1 ) + '">Waiting...</p>' );
// Add this row to 2D array
modelsArr.push( modelArr );
}
// Fill in sheet
gs.withSuccessHandler( getCategories )
.populateSheet( modelsArr, rowCount, columnCount );
// Prevent redirect
return false;
};
function getCategories( categories ) {
cats = categories;
console.log( 'Got categories.' );
}
function sheetCleared() {
console.log( 'The sheet and form were cleared, status list emptied, and global variables reset.' );
}
function prepareUpload( metadata ) {
console.log( metadata );
// Spreadsheet cells have 1-based index
for ( var row = 1; row <= modelCount; row++ ) {
// Model objects
var modelData = {
'modelFile' : modelFiles[ row - 1 ],
'token' : token,
'source' : 'google-spreadsheet'
},
modelFormData = new FormData();
// Add model metadata to model data object
for ( var col = 1; col <= columnCount; col++ ) {
modelData[ headers[ col ] ] = metadata[ row ][ col ];
}
// Validate metadata
if ( modelData.private !== 1 ) {
modelData.private = '';
console.log( 'Model ' + row + ' of ' + modelCount + ' is public.' );
}
if ( modelData.name.length > 48 ) {
console.log( 'Model name "' + modelData.name + '" is longer than 48 characters.' );
modelData.name = modelData.name.substring( 0, 47 );
console.log( 'It has been shortened to "' + modelData.name + '"' );
}
if ( modelData.description.length > 256 ) {
console.log( 'Model description "' + modelData.description + '" is longer than 256 characters.' );
modelData.description = modelData.description.substring( 0, 255 );
console.log( 'It has been shortened to "' + modelData.description + '"' );
}
if ( modelData.password.length > 64 ) {
console.log( 'Model password "' + modelData.password + '" is longer than 64 characters. It has been removed.' );
modelData.password = '';
}
if ( modelData.license !== '' ) {
console.log( 'Converting license slug to uid.' );
for ( var lic = 0; lic < lics.length; lic++ ) {
if ( modelData.license === lics[ lic ].slug ) {
modelData.license = lics[ lic ].uid;
break;
}
}
}
if ( modelData.category !== '' ) {
console.log( 'Converting category slug to uid.' );
for ( var cat = 0; cat < cats.length; cat ++ ) {
if ( modelData.category === cats[ cat ].slug ) {
modelData.category = cats[ cat ].uid;
break;
}
}
}
modelsPatchArr.push( {
'categories': [ modelData.category ]
});
console.log( 'Metadata validation ' + row + ' of ' + modelCount + ' complete.' );
// Add model data object properties to model FormData
for ( var prop in modelData ) {
if ( modelData.hasOwnProperty( prop ) ) {
modelFormData.append( prop, modelData[ prop ] );
}
}
// Add FormData object to array
dataArr.push( modelFormData );
}
// Upload the first model
uploadModel( dataArr[ uploadIndex ] );
}
function uploadModel( data ) {
console.log( 'Begin upload ' + ( uploadIndex + 1 ) + ' of ' + modelCount );
$( '#model' + ( uploadIndex + 1 ) ).html( 'Uploading...' );
$.ajax({
url: sketchfabApiUrl,
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function( response ) {
var uid = response.uid;
console.log( 'Upload ' + ( uploadIndex + 1 ) + ' of ' + modelCount + ' successful.');
console.log( 'Begin polling processing status. If successful, the model will be available at ' + sketchfabModelUrl + uid );
$( '#model' + ( uploadIndex + 1 ) ).html( 'Upload successful. Begin polling...' );
pollProcessingStatus( uid, uploadIndex );
if ( uploadIndex < modelCount - 1 ) {
uploadIndex++;
uploadModel( dataArr[ uploadIndex ] );
} else {
uploadIndex = 0;
}
},
error: function( response ) {
console.log( 'Upload Error!' );
console.log( JSON.stringify( response ) );
$( '#model' + ( uploadIndex + 1 ) ).html( 'Upload error!' );
}
});
}
function pollProcessingStatus( urlid, pollingIndex ) {
var url = sketchfabApiUrl + '/' + urlid + '/status?token=' + token,
errors = 0,
maxErrors = 10,
retry = 0,
maxRetries = 50,
retryTimeout = 5000,
retryTimeoutSec = retryTimeout / 1000, // seconds
complete = false;
function getStatus() {
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
success: function( response ) {
retry++;
console.log( 'Got status ' + ( pollingIndex + 1 ) + ' of ' + modelCount + '...' );
var status = response.processing;
switch( status ) {
case 'PENDING':
console.log( 'Model is in the processing queue. Waiting ' + ( retryTimeoutSec ) + ' seconds to try again...' );
$( '#model' + ( pollingIndex + 1 ) ).html( 'Model in queue...' );
break;
case 'PROCESSING':
console.log( 'Model is being processed. Waiting ' + ( retryTimeoutSec ) + ' seconds to try again...' );
$( '#model' + ( pollingIndex + 1 ) ).html( 'Model processing...' );
break;
case 'FAILED':
console.log( 'Model processing failed:' );
console.log( response.error );
$( '#model' + ( pollingIndex + 1 ) ).html( 'Model processing failed!' );
complete = true;
break;
case 'SUCCEEDED':
console.log( 'It worked!' );
console.log( sketchfabModelUrl + urlid );
complete = true;
$( '#model' + ( pollingIndex + 1 ) ).html( '<a target="_blank" href="' + sketchfabModelUrl + urlid + '">' + sketchfabModelUrl + urlid + '</a>' );
console.log( 'Preparing to patch categories...' );
patchModel( urlid, pollingIndex );
break;
default:
console.log( 'This message should never appear...something changed in the processing response. See: ' + url );
// complete = true;
// $( '#status' ).html( 'Something changed. See: ' + url );
}
if ( retry < maxRetries && !complete ) {
setTimeout( getStatus, retryTimeout );
} else if ( complete ) {
return;
} else if ( retry >= maxRetries ) {
console.log( 'Polled ' + maxRetries + ' times and it\'s still not finished...quitting' );
$( '#model' + ( pollingIndex + 1 ) ).html( 'Polled ' + maxRetries + ' times and it\'s still not finished...quitting' );
} else {
console.log( 'Something weird happened...quitting' );
$( '#model' + ( pollingIndex + 1 ) ).html( 'Something weird happened...quitting' );
}
},
error: function( response ) {
errors++;
retry++;
if ( errors < maxErrors && retry < maxRetries && !complete ) {
console.log( 'Error: ' + JSON.stringify( response ) );
console.log( 'Error getting status ( ' + errors + ' ). Trying again...' );
$( '#model' + ( pollingIndex + 1 ) ).html( 'Error getting status ( ' + errors + ' ). Trying again...' );
setTimeout( getStatus, retryTimeout );
} else {
console.log( 'Too many errors...quitting' );
$( '#model' + ( pollingIndex + 1 ) ).html( 'Too many errors...quitting' );
}
}
});
}
getStatus();
}
function patchModel( urlid, patchingIndex ) {
var patchUrl = sketchfabApiUrl + '/' + urlid + '?token=' + token,
patchData = JSON.stringify( modelsPatchArr[ patchingIndex ] );
if ( patchData.category === '' ) {
console.log( 'Model ' + ( patchingIndex + 1 ) + ' of ' + modelCount + ' has no category, skipping.' );
} else {
console.log( 'Patching model ' + ( patchingIndex + 1 ) + ' of ' + modelCount + '.' );
$.ajax({
url: patchUrl,
data: patchData,
type: 'PATCH',
contentType: 'application/json',
traditional: true,
success: function( response ) {
console.log( 'Patched.' );
},
error: function( response ) {
console.log( 'Patch Failed' );
}
});
}
}
</script>