diff --git a/package.json b/package.json index d5fab79..768d5d1 100644 --- a/package.json +++ b/package.json @@ -44,8 +44,11 @@ "webpack-dev-server": "1.14.1" }, "dependencies": { + "async": "^2.4.1", + "coffee-script": "1.10.0", + "file-saver": "^1.3.3", "jsrsasign": "5.0.12", - "superagent": "2.0.0", - "coffee-script": "1.10.0" + "jszip": "^3.1.3", + "superagent": "2.0.0" } } diff --git a/src/sharinpix.coffee b/src/sharinpix.coffee index cfdc1f8..923da96 100644 --- a/src/sharinpix.coffee +++ b/src/sharinpix.coffee @@ -1,6 +1,9 @@ jsrsasign = require 'jsrsasign' superagent = require 'superagent' url = require 'url' +async = require 'async' +JSZip = require 'jszip' +FileSaver = require 'file-saver' class Sharinpix constructor: (@options)-> @@ -54,6 +57,101 @@ class Sharinpix claims ).then (res)-> res + get_album_images: (album_id, page_done, all_done)-> + claims = { + abilities: { + "#{album_id}": { + Access: + see: true, + image_list: true + } + } + } + all_images = [] + load_page = (page)=> + @get("/albums/" + album_id + "/images?page=" + page, claims).then (page_images)-> + if (page_images.length == 0) + all_done(all_images) if all_done? + else + all_images.push.apply(all_images, page_images) + page_done(page_images) if page_done? + load_page(page + 1) + load_page(1) + download_images: (image_urls, one_done, all_done)-> + _download_image = (url, done)-> + superagent + .get(url) + # .responseType('blob') + .end((err, res)-> + if done? + done(res.body) unless err? + ) + + q = async.queue( + (task, one_done)-> + _download_image(task, one_done) + ,3 + ) + q.drain = -> + console.log 'Download q drained.' + all_done() if all_done? + + image_urls.forEach((image_url)-> + q.push(image_url, one_done) + ) + zip_files: (blobs, callback)-> + console.log 'zip_files' + zip = new JSZip() + deferreds = [] + blobs.forEach((blob)-> + zip.file("name#{Math.random().toString(36).substr(2, 5)}.jpg", blob, { binary: true }) # set name + ) + console.log 'zip =', zip + zip + .generateAsync({ type: "blob" }) + .then((content)-> + console.log 'zipped content', content + callback(content) if callback? + ) + + + zip_album: (album_id, options, callback)-> + return unless album_id? + options = {} unless options? + filename = (options.filename || 'Extract') + '.zip' + type = options.type || 'original' # USE FULL BY DEFAULT. original, full + add type: annotated but at original image size + _download_images = @download_images + _zip_files = @zip_files + @get_album_images( + album_id + ,null + ,(all_images)-> + image_urls = [] + all_images.forEach((image)-> + image_urls.push image.original_url # type of download + ) + console.log "all_images.length: #{all_images.length}" + all_blobs = [] + _download_images( + image_urls + ,(body)-> + console.log '1 file downloaded' + all_blobs.push(body) + ,-> + console.log 'All files downloaded' + # unless callback? + # callback = -> + # try -> + # fileSaverSupported = !!new Blob + # if fileSaverSupported + # FileSaver.saveAs(content, filename) + # else + # alert 'Download not supported on your browser.' + # catch e + _zip_files(all_blobs, callback) + ) + ) + token: (claims)-> claims["iss"] = @options.id token = jsrsasign.jws.JWS.sign( diff --git a/test.coffee b/test.coffee index 9f8562c..b7dc872 100644 --- a/test.coffee +++ b/test.coffee @@ -1,6 +1,17 @@ Sharinpix = require './src/sharinpix.js' -Sharinpix.upload('./sharinpix.jpg', 'super_test').then((image)-> - console.log image.public_id -, (err)-> - console.log 'ERROR !', err -) +# Sharinpix.configure('sharinpix://34edb5d0-4dbf-47e5-a11d-9ddc34a285da:qpk0XHmupySkCsIfPXw1QXsSyNm8i7tu-baS7JzmZSdufQQ@azhar.ngrok.io/api/v1'); +Sharinpix.configure('sharinpix://32953c6c-7b42-425d-a6b9-854366de3501:7uK9CSchCO3qaq465X_Oztv6xkKbuIuDUJgQ6fOyqrpoOGs@api.sharinpix.com/api/v1'); +# console.log('SharinPix configured'); +# Sharinpix.upload('./sharinpix.jpg', '00158000003NLMyAAO').then((image)-> + # console.log image.public_id +# , (err)-> + # console.log 'ERROR !', err +# ) + +sp = Sharinpix.get_instance() + +# album_id = '00158000003NLMyAAO' # >25 images +album_id = '500580000017zqEAAQ' # 7 images + +# sp.download_images() +sp.zip_album(album_id) \ No newline at end of file