From 27a8c282275e6c2f0ee3b5e692d5a1bc49f173ef Mon Sep 17 00:00:00 2001 From: Azhar Beebeejaun Date: Thu, 1 Jun 2017 16:35:09 +0400 Subject: [PATCH 1/3] wip --- package.json | 6 ++++-- src/sharinpix.coffee | 32 ++++++++++++++++++++++++++++++++ test.coffee | 18 +++++++++++++----- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index d5fab79..7f8a95f 100644 --- a/package.json +++ b/package.json @@ -44,8 +44,10 @@ "webpack-dev-server": "1.14.1" }, "dependencies": { + "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..ae5d63c 100644 --- a/src/sharinpix.coffee +++ b/src/sharinpix.coffee @@ -54,6 +54,38 @@ class Sharinpix claims ).then (res)-> res + zip_album: (album_id, options)-> + console.log 'IN ZIP !' + return unless album_id? + options = {} unless options? + console.log 'album = ' + album_id + filename = (options.filename || 'Extract') + '.zip' + type = options.type || 'original' # USE FULL BY DEFAULT. original, full + add type: annotated but at original image size + claims = { + abilities: { + "#{album_id}": { + Access: + see: true, + image_list: true + } + } + } + all_images = [] + get_all_images = new Promise((resolve, reject)-> + page = 1 + getter = -> + @get("/albums/" + album_id + "/images?page=" + page, claims).then (images)-> + if (images.length == 0) + resolve(all_images) + else + all_images.push.apply(all_images, images) + page++ + getter() + getter() + ) + get_all_images.then (res_all_images)-> + console.log 'res_all_images', res_all_images + token: (claims)-> claims["iss"] = @options.id token = jsrsasign.jws.JWS.sign( diff --git a/test.coffee b/test.coffee index 9f8562c..6205ba7 100644 --- a/test.coffee +++ b/test.coffee @@ -1,6 +1,14 @@ 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() +# sp.zip_album('00158000003NLMyAAO') +sp.zip_album('500580000017zqEAAQ') +# sp.zip_album('super_test') \ No newline at end of file From 913485a6c1b5d321e4a900d385ea83236de2bfd8 Mon Sep 17 00:00:00 2001 From: Azhar Beebeejaun Date: Fri, 2 Jun 2017 17:23:45 +0400 Subject: [PATCH 2/3] wip --- package.json | 3 +- src/sharinpix.coffee | 108 +++++++++++++++++++++++++++++++++++-------- test.coffee | 9 ++-- 3 files changed, 96 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 7f8a95f..1fc2cf8 100644 --- a/package.json +++ b/package.json @@ -44,10 +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", "jszip": "^3.1.3", + "save-file": "^1.1.3", "superagent": "2.0.0" } } diff --git a/src/sharinpix.coffee b/src/sharinpix.coffee index ae5d63c..b200ae9 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' +save_file = require 'save-file' class Sharinpix constructor: (@options)-> @@ -54,13 +57,7 @@ class Sharinpix claims ).then (res)-> res - zip_album: (album_id, options)-> - console.log 'IN ZIP !' - return unless album_id? - options = {} unless options? - console.log 'album = ' + album_id - filename = (options.filename || 'Extract') + '.zip' - type = options.type || 'original' # USE FULL BY DEFAULT. original, full + add type: annotated but at original image size + get_album_images: (album_id, page_done, all_done)-> claims = { abilities: { "#{album_id}": { @@ -71,20 +68,91 @@ class Sharinpix } } all_images = [] - get_all_images = new Promise((resolve, reject)-> - page = 1 - getter = -> - @get("/albums/" + album_id + "/images?page=" + page, claims).then (images)-> - if (images.length == 0) - resolve(all_images) - else - all_images.push.apply(all_images, images) - page++ - getter() - getter() + load_page = (page)=> + @get("/albums/" + album_id + "/images?page=" + page, claims).then (page_images)-> + if (page_images.length == 0) + all_done(all_images) + else + all_images.push.apply(all_images, page_images) + page_done(page_images) + load_page(page + 1) + load_page(1) + download_images: (image_urls, one_done, all_done)-> + console.log 'download_images' + _download_image = (url, done)-> + superagent + .get(url) + # .responseType('blob') + .end((err, res)-> + done(res.body) unless err? + ) + + q = async.queue( + (task, one_done)-> + _download_image(task, one_done) + ,3 + ) + q.drain = -> + console.log 'q drained' + all_done() + + image_urls.forEach((image_url)-> + q.push(image_url, one_done) + ) + zip_files: (blobs)-> + 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 }) + ) + # save_file( + # blobs[0], + # 'image.jpg', + # (err)=> + # console.log 'File saved !' + # ) + zip.generateAsync({ type: "blob" }) + .then((content)-> + console.log 'zipping content', content + save_file( + content, + 'Extract.zip', + (err)-> + console.log 'done?' + + ) + ) + + + zip_album: (album_id, options)-> + 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 + ,(page_images)-> + console.log "Page handling #{page_images.length}" + ,(all_images)-> + image_urls = [] + all_images.forEach((image)-> + image_urls.push image.original_url + ) + console.log "All handling #{all_images.length}" + all_blobs = [] + _download_images( + image_urls + ,(body)-> + console.log '1 file downloaded' + all_blobs.push(body) + ,-> + console.log 'All files downloaded' + _zip_files(all_blobs) + ) ) - get_all_images.then (res_all_images)-> - console.log 'res_all_images', res_all_images token: (claims)-> claims["iss"] = @options.id diff --git a/test.coffee b/test.coffee index 6205ba7..b7dc872 100644 --- a/test.coffee +++ b/test.coffee @@ -9,6 +9,9 @@ Sharinpix.configure('sharinpix://32953c6c-7b42-425d-a6b9-854366de3501:7uK9CSchCO # ) sp = Sharinpix.get_instance() -# sp.zip_album('00158000003NLMyAAO') -sp.zip_album('500580000017zqEAAQ') -# sp.zip_album('super_test') \ No newline at end of file + +# album_id = '00158000003NLMyAAO' # >25 images +album_id = '500580000017zqEAAQ' # 7 images + +# sp.download_images() +sp.zip_album(album_id) \ No newline at end of file From c25d0d00b2490d685170a4c2355a42495c73cacb Mon Sep 17 00:00:00 2001 From: Azhar Beebeejaun Date: Mon, 25 Sep 2017 10:43:46 +0400 Subject: [PATCH 3/3] wip --- package.json | 2 +- src/sharinpix.coffee | 58 +++++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 1fc2cf8..768d5d1 100644 --- a/package.json +++ b/package.json @@ -46,9 +46,9 @@ "dependencies": { "async": "^2.4.1", "coffee-script": "1.10.0", + "file-saver": "^1.3.3", "jsrsasign": "5.0.12", "jszip": "^3.1.3", - "save-file": "^1.1.3", "superagent": "2.0.0" } } diff --git a/src/sharinpix.coffee b/src/sharinpix.coffee index b200ae9..923da96 100644 --- a/src/sharinpix.coffee +++ b/src/sharinpix.coffee @@ -3,7 +3,7 @@ superagent = require 'superagent' url = require 'url' async = require 'async' JSZip = require 'jszip' -save_file = require 'save-file' +FileSaver = require 'file-saver' class Sharinpix constructor: (@options)-> @@ -71,20 +71,20 @@ class Sharinpix load_page = (page)=> @get("/albums/" + album_id + "/images?page=" + page, claims).then (page_images)-> if (page_images.length == 0) - all_done(all_images) + all_done(all_images) if all_done? else all_images.push.apply(all_images, page_images) - page_done(page_images) + page_done(page_images) if page_done? load_page(page + 1) load_page(1) download_images: (image_urls, one_done, all_done)-> - console.log 'download_images' _download_image = (url, done)-> superagent .get(url) # .responseType('blob') .end((err, res)-> - done(res.body) unless err? + if done? + done(res.body) unless err? ) q = async.queue( @@ -93,39 +93,29 @@ class Sharinpix ,3 ) q.drain = -> - console.log 'q drained' - all_done() + console.log 'Download q drained.' + all_done() if all_done? image_urls.forEach((image_url)-> q.push(image_url, one_done) ) - zip_files: (blobs)-> + 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 }) + zip.file("name#{Math.random().toString(36).substr(2, 5)}.jpg", blob, { binary: true }) # set name ) - # save_file( - # blobs[0], - # 'image.jpg', - # (err)=> - # console.log 'File saved !' - # ) - zip.generateAsync({ type: "blob" }) + console.log 'zip =', zip + zip + .generateAsync({ type: "blob" }) .then((content)-> - console.log 'zipping content', content - save_file( - content, - 'Extract.zip', - (err)-> - console.log 'done?' - - ) + console.log 'zipped content', content + callback(content) if callback? ) - zip_album: (album_id, options)-> + zip_album: (album_id, options, callback)-> return unless album_id? options = {} unless options? filename = (options.filename || 'Extract') + '.zip' @@ -134,14 +124,13 @@ class Sharinpix _zip_files = @zip_files @get_album_images( album_id - ,(page_images)-> - console.log "Page handling #{page_images.length}" + ,null ,(all_images)-> image_urls = [] all_images.forEach((image)-> - image_urls.push image.original_url + image_urls.push image.original_url # type of download ) - console.log "All handling #{all_images.length}" + console.log "all_images.length: #{all_images.length}" all_blobs = [] _download_images( image_urls @@ -150,7 +139,16 @@ class Sharinpix all_blobs.push(body) ,-> console.log 'All files downloaded' - _zip_files(all_blobs) + # 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) ) )