-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathPersistentStorageLocalFS.ts
More file actions
221 lines (193 loc) · 6.55 KB
/
PersistentStorageLocalFS.ts
File metadata and controls
221 lines (193 loc) · 6.55 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import fs from 'fs'
import fsp from 'fs/promises'
import path from 'path'
import { pipeline } from 'stream/promises'
import { randomUUID } from 'crypto'
import type { AccessList } from '../../@types/AccessList.js'
import type {
DockerMountObject,
PersistentStorageLocalFSOptions,
PersistentStorageObject
} from '../../@types/PersistentStorage.js'
import {
CreateBucketResult,
PersistentStorageBucketRecord,
PersistentStorageFactory,
PersistentStorageFileInfo
} from './PersistentStorageFactory.js'
import { OceanNode } from '../../OceanNode.js'
import { CORE_LOGGER } from '../../utils/logging/common.js'
export class PersistentStorageLocalFS extends PersistentStorageFactory {
/* eslint-disable security/detect-non-literal-fs-filename -- localfs backend operates on filesystem paths */
private baseFolder: string
constructor(node: OceanNode) {
super(node)
const options = node.getConfig().persistentStorage
.options as PersistentStorageLocalFSOptions
this.baseFolder = options.folder
// Ensure base folder exists and is a directory (sync to avoid startup races).
try {
fs.mkdirSync(this.baseFolder, { recursive: true })
const st = fs.statSync(this.baseFolder)
if (!st.isDirectory()) {
throw new Error(
`Persistent storage folder is not a directory: ${this.baseFolder}`
)
}
fs.mkdirSync(path.join(this.baseFolder, 'buckets'), { recursive: true })
} catch (e: any) {
if (e?.code === 'EACCES') {
throw new Error(
`Persistent storage folder is not accessible (EACCES): ${this.baseFolder}. ` +
`Configure 'persistentStorage.options.folder' to a writable path inside the container and mount it as a volume.`
)
}
throw e
}
}
private bucketPath(bucketId: string): string {
return path.join(this.baseFolder, 'buckets', bucketId)
}
private async ensureBucketExists(bucketId: string): Promise<void> {
this.validateBucket(bucketId)
const bucketsRoot = path.resolve(this.baseFolder, 'buckets')
const resolvedBucketPath = path.resolve(this.bucketPath(bucketId))
if (
resolvedBucketPath !== bucketsRoot &&
!resolvedBucketPath.startsWith(bucketsRoot + path.sep)
) {
throw new Error('Invalid bucketId')
}
const row = await this.dbGetBucket(bucketId)
if (!row) {
throw new Error(`Bucket not found: ${bucketId}`)
}
}
private async ensureFileExists(bucketId: string, fileName: string): Promise<void> {
if (!fileName || fileName.includes('/') || fileName.includes('\\')) {
throw new Error('Invalid fileName')
}
const targetPath = path.join(this.bucketPath(bucketId), fileName)
try {
const st = await fsp.stat(targetPath)
if (!st.isFile()) {
throw new Error(`File not found: ${fileName}`)
}
} catch {
throw new Error(`File not found: ${fileName}`)
}
}
// eslint-disable-next-line require-await
async listBuckets(owner: string): Promise<PersistentStorageBucketRecord[]> {
return super.listBuckets(owner)
}
async createNewBucket(
accessList: AccessList[],
owner: string
): Promise<CreateBucketResult> {
const bucketId = randomUUID()
const createdAt = Math.floor(Date.now() / 1000)
const path = this.bucketPath(bucketId)
CORE_LOGGER.debug(`Creating ${path} folder for new bucket`)
await fsp.mkdir(path)
await super.dbUpsertBucket(
bucketId,
owner,
JSON.stringify(accessList ?? []),
createdAt
)
return { bucketId, owner, accessList }
}
async listFiles(
bucketId: string,
consumerAddress: string
): Promise<PersistentStorageFileInfo[]> {
await this.ensureBucketExists(bucketId)
await this.assertConsumerAllowedForBucket(consumerAddress, bucketId)
const dir = this.bucketPath(bucketId)
const entries = await fsp.readdir(dir, { withFileTypes: true })
const out: PersistentStorageFileInfo[] = []
for (const ent of entries) {
if (!ent.isFile()) continue
const filePath = path.join(dir, ent.name)
const st = await fsp.stat(filePath)
out.push({
bucketId,
name: ent.name,
size: st.size,
lastModified: Math.floor(st.mtimeMs)
})
}
return out
}
async uploadFile(
bucketId: string,
fileName: string,
content: NodeJS.ReadableStream,
consumerAddress: string
): Promise<PersistentStorageFileInfo> {
await this.ensureBucketExists(bucketId)
await this.assertConsumerAllowedForBucket(consumerAddress, bucketId)
if (!fileName || fileName.includes('/') || fileName.includes('\\')) {
throw new Error('Invalid fileName')
}
const targetDir = this.bucketPath(bucketId)
await fsp.mkdir(targetDir, { recursive: true })
const targetPath = path.join(targetDir, fileName)
await pipeline(content, fs.createWriteStream(targetPath))
const st = await fsp.stat(targetPath)
return {
bucketId,
name: fileName,
size: st.size,
lastModified: Math.floor(st.mtimeMs)
}
}
async deleteFile(
bucketId: string,
fileName: string,
consumerAddress: string
): Promise<void> {
await this.ensureBucketExists(bucketId)
await this.assertConsumerAllowedForBucket(consumerAddress, bucketId)
await this.ensureFileExists(bucketId, fileName)
const targetPath = path.join(this.bucketPath(bucketId), fileName)
await fsp.rm(targetPath)
}
async getFileObject(
bucketId: string,
fileName: string,
consumerAddress: string
): Promise<PersistentStorageObject> {
await this.ensureBucketExists(bucketId)
await this.assertConsumerAllowedForBucket(consumerAddress, bucketId)
await this.ensureFileExists(bucketId, fileName)
// This is intentionally not a downloadable URL; compute backends can interpret this object.
const obj: PersistentStorageObject = {
type: 'nodePersistentStorage',
bucketId,
fileName
}
return obj
}
async getDockerMountObject(
bucketId: string,
fileName: string,
consumerAddress?: string
): Promise<DockerMountObject> {
await this.ensureBucketExists(bucketId)
if (consumerAddress) {
await this.assertConsumerAllowedForBucket(consumerAddress, bucketId)
}
await this.ensureFileExists(bucketId, fileName)
const source = path.join(this.bucketPath(bucketId), fileName)
const target = path.posix.join('/data', 'persistentStorage', bucketId, fileName)
return {
Type: 'bind',
Source: source,
Target: target,
ReadOnly: true
}
}
}
/* eslint-enable security/detect-non-literal-fs-filename */