-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
118 lines (92 loc) · 2.58 KB
/
server.js
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
import * as dotenv from 'dotenv'
import * as fs from 'fs'
import AWS from 'aws-sdk'
import Path from 'path'
import Request from 'request'
import Stream from 'stream'
import { getImages } from 'icloud-shared-album'
dotenv.config()
const FILENAME = 'photos.json'
const FOLDER = process.env.FOLDER
const S3_ID = process.env.S3_ID
const S3_SECRET = process.env.S3_SECRET
const BUCKET_NAME = process.env.S3_BUCKET_NAME
class Cloud {
constructor (albumID) {
this.albumID = albumID
this.S3 = new AWS.S3({
accessKeyId: S3_ID,
secretAccessKey: S3_SECRET
})
if (!fs.existsSync(FILENAME)) {
console.error(`File ${FILENAME} not found`)
return
}
this.uploadedItems = JSON.parse(fs.readFileSync(FILENAME))
this.start()
}
start () {
getImages(this.albumID).then(this.onGetData.bind(this))
}
writeUploadedFilenames (filenames) {
this.uploadedItems = this.uploadedItems.concat(filenames)
fs.writeFile(FILENAME, JSON.stringify(this.uploadedItems), (error, data) => {
if (error) {
throw(error)
}
console.log('OK')
})
}
extractFilenameFromURL (URL) {
return Path.basename(URL).split('?')[0]
}
getS3BucketInfo (filename, URL) {
const ext = Path.extname(filename).substring(1)
const Body = new Stream.PassThrough()
Request(URL).pipe(Body)
return {
Bucket: BUCKET_NAME,
Key: `${FOLDER}/${filename}`,
ContentType:`image/${ext}`,
Body
}
}
uploadImage (photoID, URL) {
return new Promise((resolve, reject) => {
const filename = this.extractFilenameFromURL(URL)
this.S3.upload(this.getS3BucketInfo(filename, URL), (error, data) => {
if (error) {
return reject(error)
}
resolve(filename)
})
})
}
onGetData (data) {
if (!data || !data.photos.length) {
return
}
let promises = []
data.photos.forEach(async (photo) => {
let size = 0
let selectedVersion = undefined
Object.values(photo.derivatives).forEach((version) => {
if (version.fileSize > size) {
selectedVersion = version
size = selectedVersion.fileSize
}
})
if (selectedVersion) {
const URL = selectedVersion.url
const filename = this.extractFilenameFromURL(URL)
if (!this.uploadedItems.includes(filename)) {
promises.push(this.uploadImage(photo.photoGuid, URL))
}
}
})
Promise.all(promises).then((filenames) => {
this.writeUploadedFilenames(filenames)
})
}
}
const cloud = new Cloud(process.env.ALBUM_ID)