-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstation_insert_center_image.js
75 lines (60 loc) · 1.64 KB
/
station_insert_center_image.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
//存取文件的类
var fs = require('fs')
//处理路径的类
var path = require('path')
//存放配置文件
var config = require('./config')
var mongodb = require('mongodb');
//获取Post数据
var pathfrom
var filename
//Promise函数,用来获取第一个待传文件名称
var getFirstFile = function () {
return new Promise(function (resolve, reject) {
//获取第一个待传文件名称
fs.readdir(config.local.center_image_path, function (err, files) {
if (files.length > 0) {
var filePath = path.normalize(config.local.center_image_path + files[0])
//将待传文件名称存储到全局变量
pathfrom = filePath
filename = files[0]
resolve()
}
else {
reject(-1)
}
}
)
}
)
}
//向mongoDB中存储车道传上来的图片
var insertImageToMongo = function () {
return new Promise(function (resolve, reject) {
mongodb.MongoClient.connect(config.local.mongo_url, function (err, db) {
var bucket = new mongodb.GridFSBucket(db, {
bucketName: config.local.image.mongo_collection
})
fs.createReadStream(pathfrom).pip(bucket.openUploadStream(filename)).on('err', function () {
if (err) {
reject(err)
}
}).on('finish', function () {
resolve()
})
})
})
}
var search = function () {
getFirstFile().then(insertImageToMongo).then(function () {
fs.unlinkSync(pathfrom)
setInterval(function () {
search()
}, 10000)
}).catch(function () {
setInterval(function () {
search()
}, 10000)
})
}
search()