Created
July 24, 2013 19:11
-
-
Save jonahkagan/6073534 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
db = require 'clever-db' | |
async = require 'async' | |
_ = require 'underscore' | |
quest = require 'quest' | |
count = 0 | |
OAuthClient = db.connection.model 'OAuthClient' | |
OAuthClient.find { type: 'app', 'data.image': { $exists: true }}, (err, clients) -> | |
console.log err if err? | |
console.log 'clients:', clients.length | |
async.forEach( | |
_(clients).chain() | |
.map((client) -> [ | |
client: client | |
url: client.data.image.small | |
prop: 'small' | |
, | |
client: client | |
url: client.data.image.large | |
prop: 'large' | |
]) | |
.flatten() | |
.filter((image) -> image.url?) | |
#.take(1) # testing | |
#.tap(console.log) | |
.value() | |
, (image, cb_fe) -> | |
unless /^http/.test image.url | |
image.url = "https://account.getclever.com#{image.url}" | |
console.log 'storing', image.url | |
quest | |
method: 'POST' | |
uri: 'https://www.filepicker.io/api/store/S3' | |
qs: key: 'AkFAhd5DmTiachOXSnEiNz' | |
form: url: image.url | |
, (err, resp, body) -> | |
try | |
if err then throw err | |
body = JSON.parse body | |
catch e | |
console.log image | |
console.log e | |
return cb_fe() | |
s3_url = "https://clever-partner-images.s3.amazonaws.com/#{body.key}" | |
console.log 's3:', s3_url | |
count += 1 | |
updateObj = $set: {} | |
updateObj.$set["data.image.#{image.prop}"] = s3_url | |
image.client.update updateObj, cb_fe | |
, (err) -> | |
console.log "done foreach" | |
console.log err if err? | |
console.log "saved images: ", count | |
process.exit() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment