forked from reruin/sharelist
-
Notifications
You must be signed in to change notification settings - Fork 2
/
drive.fs.js
138 lines (110 loc) · 3 KB
/
drive.fs.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
138
/**
* Mount file system
*/
const path = require('path')
const fs = require('fs')
const os = require('os')
const isWinOS = os.platform() == 'win32'
/**
* Convert posix style path to windows style
*
* @param {string} [p]
* @return {string}
*/
const winStyle = (p) => p.replace(/^\/([^\/]+?)/, '$1:\\').replace(/\//g, '\\').replace(/(?<!\:)\\+$/, '').replace(/\\{2,}/g, '\\')
/**
* Convert windows style path to posix style
*
* @param {string} [p]
* @return {string}
*/
const posixStyle = (p) => p.split('\\').join('/').replace(/^([a-z])\:/i,'/$1')
/**
* normalize path(posix style) and replace current path
*
* @param {string} [p]
* @return {string}
*/
const normalize = (p) => path.posix.normalize(p.replace(/^\.\//, slpath(process.cwd()) + '/'))
const slpath = (p) => (isWinOS ? posixStyle(p) : p)
const realpath = (p) => (isWinOS ? winStyle(p) : p)
class FileSystem {
constructor() {
this.name = 'FileSystem'
this.label = '本地文件'
this.mountable = true
this.cache = false
this.version = '1.0'
this.protocol = 'fs'
}
mkdir(p) {
if (fs.existsSync(p) == false) {
this.mkdir(path.dirname(p));
fs.mkdirSync(p);
}
}
path(id) {
let { datetime, extname } = this.helper
let { protocol } = this
let dir = normalize(id)
let realdir = realpath(dir)
let stat = fs.statSync(realdir)
if (stat.isDirectory()) {
let children = []
fs.readdirSync(realdir).forEach((filename) => {
let path = normalize(dir + '/' + filename)
let stat
try {
stat = fs.statSync(realpath(path))
} catch (e) {}
let obj = {
id: path,
name: filename,
protocol: this.protocol,
type: 'other'
}
if (stat) {
obj.created_at = datetime(stat.ctime)
obj.updated_at = datetime(stat.mtime)
if (stat.isDirectory()) {
obj.type = 'folder'
} else if (stat.isFile()) {
obj.ext = extname(filename)
obj.size = stat.size
}
}
children.push(obj)
})
return { id: dir, type: 'folder', protocol: this.protocol , children }
} else if (stat.isFile()) {
return {
id,
name: path.basename(id),
protocol: this.protocol,
ext: extname(id),
url: realpath(id),
size: stat.size,
outputType: 'file',
proxy: true
}
} else {
return false
}
}
folder(id) {
return this.path(id)
}
file(id) {
return this.path(id)
}
async createReadStream({ id, options = {} } = {}) {
return fs.createReadStream(realpath(id), { ...options, highWaterMark: 64 * 1024 })
}
async createWriteStream({ id, options = {}, target = '' } = {}) {
let fullpath = path.join(id , target)
let parent = (fullpath.split('/').slice(0, -1).join('/') + '/').replace(/\/+$/g, '/')
this.mkdir(parent)
return fs.createWriteStream(realpath(fullpath), options)
}
}
module.exports = FileSystem