Last active
November 16, 2017 07:00
-
-
Save goofmint/6157cf3289108d931b8fbd8aa92eab30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadScript(src) { | |
return new Promise(function(res, rej) { | |
try { | |
var done = false; | |
var head = document.getElementsByTagName('head')[0]; | |
var script = document.createElement('script'); | |
script.src = src; | |
head.appendChild(script); | |
// Attach handlers for all browsers | |
script.onload = script.onreadystatechange = function() { | |
if ( !done && (!this.readyState || | |
this.readyState === "loaded" || this.readyState === "complete") ) { | |
done = true; | |
res(); | |
// Handle memory leak in IE | |
script.onload = script.onreadystatechange = null; | |
if ( head && script.parentNode ) { | |
head.removeChild( script ); | |
} | |
} | |
}; | |
}catch(e) { | |
rej(e); | |
} | |
}); | |
} | |
var app_id = location.href.replace(/.*applications\/(.*?)\/.*/, "$1"); | |
var session = JSON.parse(getCookie("session")).sessionToken; | |
let baseUrl = `https://console.mb.api.cloud.nifty.com/2013-09-01/applications/${app_id}/files`; | |
let limit = 1000; | |
Promise.all([ | |
loadScript('https://code.jquery.com/jquery-3.2.1.min.js'), | |
loadScript('https://cdn.rawgit.com/eligrey/FileSaver.js/master/FileSaver.min.js') | |
]) | |
.then(function() { | |
const getList = (skip) => { | |
let url = `${baseUrl}?skip=${skip}&limit=${limit}`; | |
$.ajax({ | |
url: url, | |
headers: {"X-NCMB-Devs-Session-Token": session}, | |
type: 'GET' | |
}) | |
.then((data) => { | |
return download(data.results) | |
}) | |
.then((files) => { | |
console.log(files); | |
if (files.length === limit) { | |
getList((skip + 1) + limit) | |
} | |
}) | |
}; | |
getList(0); | |
}); | |
var count = $(".filestore-list-name").length; | |
const download = (files) => { | |
return new Promise(function(res, rej) { | |
if (!files) { | |
return res(files); | |
} | |
const loop = (i) => { | |
const file = files[i]; | |
if (!file) { | |
return res(files) | |
} | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', 'https://console.mb.cloud.nifty.com/api/download/file', true); | |
xhr.responseType = 'blob'; | |
xhr.onload = function(e) { | |
// Blobで返ってくる | |
saveAs(this.response, file.fileName); | |
loop(i + 1); | |
}; | |
var data = new FormData(); | |
data.append('applicationId', app_id); | |
data.append('fileName', file.fileName); | |
data.append('sessionToken', session); | |
xhr.send(data); | |
} | |
loop(0); | |
}); | |
}; | |
function getCookie(name){ | |
var r = null; | |
var c = name + '='; | |
var allcookies = document.cookie; | |
var position = allcookies.indexOf( c ); | |
if( position != -1 ){ | |
var startIndex = position + c.length; | |
var endIndex = allcookies.indexOf( ';', startIndex ); | |
if( endIndex == -1 ){ | |
endIndex = allcookies.length; | |
} | |
r = decodeURIComponent(allcookies.substring( startIndex, endIndex ) ); | |
} | |
return r; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment