-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpersistentStorage.ts
More file actions
201 lines (189 loc) · 7.03 KB
/
persistentStorage.ts
File metadata and controls
201 lines (189 loc) · 7.03 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
import express from 'express'
import { Readable } from 'stream'
import { SERVICES_API_BASE_PATH, PROTOCOL_COMMANDS } from '../../utils/constants.js'
import { HTTP_LOGGER } from '../../utils/logging/common.js'
import { streamToObject, streamToString } from '../../utils/util.js'
import {
PersistentStorageCreateBucketHandler,
PersistentStorageDeleteFileHandler,
PersistentStorageGetBucketsHandler,
PersistentStorageGetFileObjectHandler,
PersistentStorageListFilesHandler,
PersistentStorageUploadFileHandler
} from '../core/handler/persistentStorage.js'
export const persistentStorageRoutes = express.Router()
// Create bucket
persistentStorageRoutes.post(
`${SERVICES_API_BASE_PATH}/persistentStorage/buckets`,
express.json(),
async (req, res) => {
try {
const response = await new PersistentStorageCreateBucketHandler(
req.oceanNode
).handle({
...req.body,
command: PROTOCOL_COMMANDS.PERSISTENT_STORAGE_CREATE_BUCKET,
authorization: req.headers?.authorization,
caller: req.caller
})
if (!response.stream) {
res.status(response.status.httpStatus).send(response.status.error)
return
}
const payload = await streamToObject(response.stream as Readable)
res.status(200).json(payload)
} catch (error) {
HTTP_LOGGER.error(`PersistentStorage create bucket error: ${error}`)
res.status(500).send('Internal Server Error')
}
}
)
// List buckets for an owner (then filtered by ACL in handler)
persistentStorageRoutes.get(
`${SERVICES_API_BASE_PATH}/persistentStorage/buckets`,
async (req, res) => {
try {
const response = await new PersistentStorageGetBucketsHandler(req.oceanNode).handle(
{
command: PROTOCOL_COMMANDS.PERSISTENT_STORAGE_GET_BUCKETS,
consumerAddress: req.query.consumerAddress as string,
signature: req.query.signature as string,
nonce: req.query.nonce as string,
chainId: parseInt(req.query.chainId as string) || null,
owner: req.query.owner as string,
authorization: req.headers?.authorization,
caller: req.caller
} as any
)
if (!response.stream) {
res.status(response.status.httpStatus).send(response.status.error)
return
}
const payload = await streamToObject(response.stream as Readable)
res.status(200).json(payload)
} catch (error) {
HTTP_LOGGER.error(`PersistentStorage get buckets error: ${error}`)
res.status(500).send('Internal Server Error')
}
}
)
// List files in bucket
persistentStorageRoutes.get(
`${SERVICES_API_BASE_PATH}/persistentStorage/buckets/:bucketId/files`,
async (req, res) => {
try {
const response = await new PersistentStorageListFilesHandler(req.oceanNode).handle({
command: PROTOCOL_COMMANDS.PERSISTENT_STORAGE_LIST_FILES,
consumerAddress: req.query.consumerAddress as string,
signature: req.query.signature as string,
nonce: req.query.nonce as string,
bucketId: req.params.bucketId,
authorization: req.headers?.authorization,
caller: req.caller
} as any)
if (!response.stream) {
res.status(response.status.httpStatus).send(response.status.error)
return
}
const payload = await streamToObject(response.stream as Readable)
res.status(200).json(payload)
} catch (error) {
HTTP_LOGGER.error(`PersistentStorage list files error: ${error}`)
res.status(500).send('Internal Server Error')
}
}
)
// Get file object for a file in a bucket
persistentStorageRoutes.get(
`${SERVICES_API_BASE_PATH}/persistentStorage/buckets/:bucketId/files/:fileName/object`,
async (req, res) => {
try {
const response = await new PersistentStorageGetFileObjectHandler(
req.oceanNode
).handle({
command: PROTOCOL_COMMANDS.PERSISTENT_STORAGE_GET_FILE_OBJECT,
consumerAddress: req.query.consumerAddress as string,
signature: req.query.signature as string,
nonce: req.query.nonce as string,
bucketId: req.params.bucketId,
fileName: req.params.fileName,
authorization: req.headers?.authorization,
caller: req.caller
} as any)
if (!response.stream) {
res.status(response.status.httpStatus).send(response.status.error)
return
}
const payload = await streamToObject(response.stream as Readable)
res.status(200).json(payload)
} catch (error) {
HTTP_LOGGER.error(`PersistentStorage get file object error: ${error}`)
res.status(500).send('Internal Server Error')
}
}
)
// Upload file to bucket. Body is treated as raw bytes.
persistentStorageRoutes.post(
`${SERVICES_API_BASE_PATH}/persistentStorage/buckets/:bucketId/files/:fileName`,
async (req, res) => {
try {
const response = await new PersistentStorageUploadFileHandler(req.oceanNode).handle(
{
command: PROTOCOL_COMMANDS.PERSISTENT_STORAGE_UPLOAD_FILE,
consumerAddress: req.query.consumerAddress as string,
signature: req.query.signature as string,
nonce: req.query.nonce as string,
bucketId: req.params.bucketId,
fileName: req.params.fileName,
// Stream request body directly (supports chunked uploads, avoids buffering).
stream: req,
authorization: req.headers?.authorization,
caller: req.caller
} as any
)
if (!response.stream) {
res.status(response.status.httpStatus).send(response.status.error)
return
}
const payload = await streamToObject(response.stream as Readable)
res.status(200).json(payload)
} catch (error) {
HTTP_LOGGER.error(`PersistentStorage upload error: ${error}`)
res.status(500).send('Internal Server Error')
}
}
)
// Delete file from bucket
persistentStorageRoutes.delete(
`${SERVICES_API_BASE_PATH}/persistentStorage/buckets/:bucketId/files/:fileName`,
async (req, res) => {
try {
const response = await new PersistentStorageDeleteFileHandler(req.oceanNode).handle(
{
command: PROTOCOL_COMMANDS.PERSISTENT_STORAGE_DELETE_FILE,
consumerAddress: req.query.consumerAddress as string,
signature: req.query.signature as string,
nonce: req.query.nonce as string,
chainId: parseInt(req.query.chainId as string) || null,
bucketId: req.params.bucketId,
fileName: req.params.fileName,
authorization: req.headers?.authorization,
caller: req.caller
} as any
)
if (response.status.httpStatus !== 200) {
res.status(response.status.httpStatus).send(response.status.error)
return
}
if (!response.stream) {
res.status(200).json({ success: true })
return
}
const payload = JSON.parse(await streamToString(response.stream as Readable))
res.status(200).json(payload)
} catch (error) {
HTTP_LOGGER.error(`PersistentStorage delete error: ${error}`)
res.status(500).send('Internal Server Error')
}
}
)