This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathget-files-stream.js
137 lines (113 loc) · 2.86 KB
/
get-files-stream.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict'
const isNode = require('detect-node')
const Multipart = require('multipart-stream')
const flatmap = require('flatmap')
const escape = require('glob-escape')
function headers (file) {
const name = file.path || ''
const header = {
'Content-Disposition': `file; filename="${name}"`
}
if (file.dir || !file.content) {
header['Content-Type'] = 'application/x-directory'
} else if (file.symlink) {
header['Content-Type'] = 'application/symlink'
} else {
header['Content-Type'] = 'application/octet-stream'
}
return header
}
function strip (name, base) {
const smallBase = base
.split('/')
.slice(0, -1)
.join('/') + '/'
return name.replace(smallBase, '')
}
function loadPaths (opts, file) {
const path = require('path')
const fs = require('fs')
const glob = require('glob')
const followSymlinks = opts.followSymlinks != null ? opts.followSymlinks : true
file = path.resolve(file)
const stats = fs.statSync(file)
if (stats.isDirectory() && !opts.recursive) {
throw new Error('Can only add directories using --recursive')
}
if (stats.isDirectory() && opts.recursive) {
const mg = new glob.sync.GlobSync(`${escape(file)}/**/*`, {
follow: followSymlinks
})
return mg.found
.map((name) => {
// symlinks
if (mg.symlinks[name] === true) {
return {
path: strip(name, file),
symlink: true,
dir: false,
content: fs.readlinkSync(name)
}
}
// files
if (mg.cache[name] === 'FILE') {
return {
path: strip(name, file),
symlink: false,
dir: false,
content: fs.createReadStream(name)
}
}
// directories
if (mg.cache[name] === 'DIR' || mg.cache[name] instanceof Array) {
return {
path: strip(name, file),
symlink: false,
dir: true
}
}
// files inside symlinks and others
return
})
// filter out null files
.filter(Boolean)
}
return {
path: file,
content: fs.createReadStream(file)
}
}
function getFilesStream (files, opts) {
if (!files) {
return null
}
const mp = new Multipart()
flatmap(files, (file) => {
if (typeof file === 'string') {
if (!isNode) {
throw new Error('Can not add paths in node')
}
return loadPaths(opts, file)
}
if (file.path && !file.content) {
file.dir = true
return file
}
if (file.path && (file.content || file.dir)) {
return file
}
return {
path: '',
symlink: false,
dir: false,
content: file
}
}).forEach((file) => {
mp.addPart({
headers: headers(file),
body: file.content
})
})
return mp
}
exports = module.exports = getFilesStream