From e07e36d4407e51d37912ca3c292f74105a37257d Mon Sep 17 00:00:00 2001 From: Sjors van Holst Date: Thu, 1 Dec 2022 19:24:11 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Moved=20Spaces=20functions=20to=20U?= =?UTF-8?q?berdeno?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/spacesClient.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/services/spacesClient.ts b/services/spacesClient.ts index 0469497..585b3b9 100644 --- a/services/spacesClient.ts +++ b/services/spacesClient.ts @@ -49,6 +49,43 @@ class SpacesClient { }); } + async getFile(filename: string) { + const response = await this.bucket.listObjects({ prefix: filename }); + const contents = response?.contents; + + if (!contents || contents.length === 0) { + return null; + } + + return { + name: contents[0].key!.replace(/^.*\/(.*)$/, "$1"), + size: contents[0].size!, + updated: contents[0].lastModified!, + download: spacesClient.signedGET(contents[0].key!), + }; + } + + async getFiles(directory: string) { + const response = await this.bucket.listObjects({ prefix: directory }); + const contents = response?.contents; + + if (!contents || contents.length === 0) { + return null; + } + + // The first item is just the directory instead of a file so we'll remove it + contents.shift(); + + return contents.map((content) => { + return { + name: content.key!.replace(/^.*\/(.*)$/, "$1"), + size: content.size!, + updated: content.lastModified!, + download: spacesClient.signedGET(content.key!), + }; + }); + } + async deleteFile(filename: string) { return await this.bucket.deleteObject(filename); }