forked from ss1121/FKP-REST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavefile.js
More file actions
41 lines (34 loc) · 1.15 KB
/
savefile.js
File metadata and controls
41 lines (34 loc) · 1.15 KB
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
var fs = require('fs')
var request = require('request')
var config = require('config.js')
var path = require('path')
var libs = require('libs/libs')
function *savefile(file, path2save){
libs.clog('savefile to local '+__filename)
try {
if(!path2save){
path2save = config.avatar_root
}
if (!fs.existsSync(path2save)) {
fs.mkdirSync(path2save);
fs.chmodSync(path2save, '0777')
}
if (file && file.indexOf('http')===0){
var _file = path.parse(file)
// { root: '',
// dir: 'http://www.163.com/abc',
// base: '123.jpg',
// ext: '.jpg',
// name: '123' }
var _to = path2save + '/' + _file.base
// sample https://github.com/koajs/koa/issues/102
// yield pipe(part, fs.createWriteStream("wherever"))
// return { location: "wherever", filename: part.filename }
yield request.get(file).pipe( fs.createWriteStream(_to) );
return { filename: _to }
}
} catch (e) {
console.log('module/savefile' + e);
}
}
module.exports = savefile