getRootFolderID callback request drive about get err req res if err ca

  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
getRootFolderID = (callback) ->
request = drive.about.get((err, req, res) ->
if err
callback null
else
callback res.body.rootFolderId
return
)
return
createFolder = (parentId, title, callback) ->
drive.files.insert
resource:
parents: [id: parentId]
title: title
mimeType: "application/vnd.google-apps.folder"
, (e, rq, rs) ->
return callback(null) if e or not rs.body or not rs.body.id
console.log "folder \"" + title + "\" created with id:", rs.body.id
callback rs.body
return
getOrCreateFolder = (parentId, title, callback) ->
drive.files.list
q: "title=\"" + title + "\" and mimeType=\"application/vnd.google-apps.folder\" and \"" + parentId + "\" in parents"
, (e, rq, rs) ->
for i of rs.body.items
item = rs.body.items[i]
continue if item.labels.trashed
return callback(item)
console.log "folder \"" + title + "\" does not exist, creating new one"
createFolder parentId, title, callback
return
return
uploadFileToFolder = (folder, file, callback) ->
mimeType = mime.lookup(file.fullpath)
console.log "Detected mime type:", mimeType
request =
resource:
parents: [folder]
title: file.filename
mimeType: mimeType
media:
mimeType: mimeType
body: fs.createReadStream(file.fullpath)
console.log "performing files.insert request:", request
drive.files.insert request, (e, rq, rs) ->
callback e, rs.body
return
return
uploadToGoogleDrive = (file, callback) ->
getRootFolderID (rootFolderId) ->
console.log "got root folder id:", rootFolderId
getOrCreateFolder rootFolderId, "Photos", (photosFolder) ->
unless photosFolder
console.error "failed to create /Photos"
return callback(null)
console.log "got /Photos folder id:", photosFolder.id
getOrCreateFolder photosFolder.id, "Photostream", (photostreamFolder) ->
unless photostreamFolder
console.error "failed to create /Photos/Photostream"
return callback(null)
console.log "got /Photos/Photostream folder id:", photostreamFolder.id
console.log "getting creation date for file", file.fullpath
getPhotoDate file, (date) ->
date = "Unsorted" unless date
console.log "got photo date:", date
getOrCreateFolder photostreamFolder.id, date, (dateFolder) ->
unless dateFolder
console.error "failed to create /Photos/Photostream/" + date
return callback(null)
console.log "got photostream date folder with id:", dateFolder.id
uploadFileToFolder dateFolder, file, callback
return
return
return
return
return
return
extractExifCreationDate = (file, callback) ->
try
new ExifImage(
image: file.fullpath
, ((error, exifData) ->
if error
console.log "Failed to extract photo date from exif: " + error.message
callback null
else if not exifData or not exifData.exif or not exifData.exif.CreateDate
callback null
else
callback exifData.exif.CreateDate
return
).bind(this))
catch error
console.log "Error:", error.message
callback null
return
getPhotoDate = (file, callback) ->
extractExifCreationDate file, ((creationDate) ->
unless creationDate
callback creationDate
else
date = moment(creationDate, "YYYY:MM:DD HH:mm:ss")
return callback(null) unless date.isValid()
callback date.format("YYYY MM MMMM")
return
).bind(this)
return
googleapis = require("googleapis")
mime = require("mime")
fs = require("fs")
moment = require("moment")
ExifImage = require("exif").ExifImage
CLIENT_ID = "..."
CLIENT_SECRET = "..."
REDIRECT_URL = "urn:ietf:wg:oauth:2.0:oob"
SCOPE = "https://www.googleapis.com/auth/drive"
AUTH_CODE = "..."
googleDrive = require("google-drive")
TOKENS =
access_token: "..."
token_type: "Bearer"
refresh_token: "..."
expiry_date: ""
auth = new googleapis.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL)
auth.setCredentials TOKENS
googleapis.options auth: auth
drive = googleapis.drive(version: "v2")
require("osx-photostream")().on "update", (file) ->
console.log "New image: " + file.filename
uploadToGoogleDrive file, (error, result) ->
if error
console.error "upload failed:", error
else
console.log "upload succeeded"
return
return