This repository was archived by the owner on Oct 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
216 lines (193 loc) · 5.22 KB
/
storage.js
File metadata and controls
216 lines (193 loc) · 5.22 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
const https = require("https");
const { URL } = require("url");
var URLModule = require("url");
class GvbBaseSupabaseStorage {
constructor(bucket, projectUrl, apiKey) {
if (!bucket || !projectUrl || !apiKey) {
throw new Error("Missing bucket name, project URL, or API key");
}
this.bucket = bucket;
this.projectUrl = projectUrl.replace(/\/+$/, ""); // trim trailing slashes
this.apiKey = apiKey;
}
_makeRequest(method, path, headers = {}, body = null) {
return new Promise((resolve, reject) => {
const url = new URL(path, this.projectUrl);
const options = {
method,
hostname: url.hostname,
path: url.pathname + url.search,
headers: {
apikey: this.apiKey,
Authorization: `Bearer ${this.apiKey}`,
...headers,
},
};
const req = https.request(options, (res) => {
const chunks = [];
res.on("data", (chunk) => chunks.push(chunk));
res.on("end", () => {
const buffer = Buffer.concat(chunks);
if (res.statusCode >= 400) {
return reject(
new Error(`HTTP ${res.statusCode}: ${buffer.toString()}`)
);
}
resolve({ buffer, response: res });
});
});
req.on("error", reject);
if (body) req.write(body);
req.end();
});
}
async getFileStatus (filename) {
const path = `/storage/v1/object/${this.bucket}/${encodeURIComponent(
filename
)}`;
const { buffer } = await this._makeRequest("GET", path);
return true;
}
async downloadFile(filename) {
const path = `/storage/v1/object/${this.bucket}/${encodeURIComponent(
filename
)}`;
const { buffer } = await this._makeRequest("GET", path);
return buffer;
}
async downloadFileAdvanced(filename) {
const path = `/storage/v1/object/${this.bucket}/${encodeURIComponent(
filename
)}`;
const { buffer, response } = await this._makeRequest("GET", path);
return {
buffer,
response,
request: null,
headers: response.headers,
status: response.statusCode,
};
}
getHeaderValue(headers, headerName) {
for (var key of Object.keys(headers)) {
if (key.toLowerCase() == headerName.toLowerCase()) {
return headers[key];
}
}
return null;
}
downloadFileResponseProxy(
filename,
_customHeaders,
serverResponse,
proxyHeaders = []
) {
return new Promise((resolve,reject) => {
const path = `/storage/v1/object/${this.bucket}/${encodeURIComponent(
filename
)}`;
var url = URLModule.parse(this.projectUrl + path);
var customHeaders = {};
if (_customHeaders) {
customHeaders = _customHeaders;
}
var _this = this;
const options = {
method: "GET",
headers: {
apikey: this.apiKey,
Authorization: `Bearer ${this.apiKey}`,
...customHeaders
},
...url
};
https
.get(options, (res) => {
for (var header of proxyHeaders) {
var value = _this.getHeaderValue(res.headers,header);
if (value) serverResponse.setHeader(header, value);
}
serverResponse.statusCode = res.statusCode;
res.pipe(serverResponse);
var data = [];
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
resolve({
buffer: Buffer.concat(data),
response: res,
headers: res.headers,
status: res.statusCode,
});
})
})
.on("error", (err) => {
reject(err);
});
})
}
async uploadFile(filename, data, contentType = "application/octet-stream") {
const path = `/storage/v1/object/${this.bucket}/${encodeURIComponent(
filename
)}`;
const { buffer } = await this._makeRequest(
"POST",
path,
{
"Content-Type": contentType,
"Content-Length": data.length,
"x-upsert": "true",
},
data
);
return buffer;
}
async uploadFileAdvanced(
filename,
data,
contentType = "application/octet-stream"
) {
const path = `/storage/v1/object/${this.bucket}/${encodeURIComponent(
filename
)}`;
const { buffer, response } = await this._makeRequest(
"POST",
path,
{
"Content-Type": contentType,
"Content-Length": data.length,
"x-upsert": "true",
},
data
);
return {
buffer,
response,
request: null,
headers: response.headers,
status: response.statusCode,
};
}
async deleteFile(filename) {
const path = `/storage/v1/object/${this.bucket}/${encodeURIComponent(
filename
)}`;
const { buffer } = await this._makeRequest("DELETE", path);
return buffer;
}
async deleteFileAdvanced(filename) {
const path = `/storage/v1/object/${this.bucket}/${encodeURIComponent(
filename
)}`;
const { buffer, response } = await this._makeRequest("DELETE", path);
return {
buffer,
response,
request: null,
headers: response.headers,
status: response.statusCode,
};
}
}
module.exports = GvbBaseSupabaseStorage;