Skip to content

Commit 2dfd741

Browse files
committed
feat(object): make hash type optional and add expiration to url parser
1 parent 400a68b commit 2dfd741

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

.changeset/blue-glasses-study.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/blob": patch
3+
---
4+
5+
Fix object url parse error when hash is not provided and add support for expiration.

src/structures/object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class BlobObject {
1212
/** The prefix of the object (Optional) */
1313
prefix?: string;
1414
/** The hash of the object */
15-
hash: string;
15+
hash?: string;
1616
/** The id of the user who created the object */
1717
userId: string;
1818
/** The file extension of the object */

src/utils/object-url.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import { SquareCloudBlobError } from "../structures/error";
22

33
const objectUrlRegex =
4-
/^(?<url>https:\/\/public-blob\.squarecloud\.dev)?\/?(?<userId>[\w\d]+\/)(?<prefix>[\w\d\-_]+\/)?(?<name>[\w\d_]+)(?:-(?<hash>[\w\d]+))?(?:-ex\d+)?\.(?<extension>\w+)$/;
4+
/^(?<url>https:\/\/public-blob\.squarecloud\.dev)?\/?(?<userId>[\w\d]+\/)(?<prefix>[\w\d\-_]+\/)?(?<name>[\w\d_]+)(?:-(?<hash>(?!ex\d+)[\w\d]+))?(?:-ex(?<expiration>\d+))?\.(?<extension>\w+)$/;
5+
6+
type ParsedObjectUrl = {
7+
id: string;
8+
userId: string;
9+
prefix?: string;
10+
name: string;
11+
hash?: string;
12+
extension: string;
13+
expiration?: number;
14+
};
515

616
/**
717
* Parses the object URL to extract id, userId, prefix, name, hash and extension.
818
*
919
* @param url - The object URL to parse.
1020
*/
11-
export function parseObjectUrl(url: string) {
21+
export function parseObjectUrl(url: string): ParsedObjectUrl {
1222
const match = url.match(objectUrlRegex);
1323

1424
if (!match?.groups) {
@@ -20,13 +30,17 @@ export function parseObjectUrl(url: string) {
2030
const name = match.groups.name;
2131
const hash = match.groups.hash;
2232
const extension = match.groups.extension;
33+
const expiration = match.groups.expiration
34+
? Number(match.groups.expiration)
35+
: undefined;
2336

2437
return {
25-
id: `${userId}/${prefix ? `${prefix}/` : ""}${name}-${hash}.${extension}`,
38+
id: `${userId}/${prefix ? `${prefix}/` : ""}${name}${hash ? `-${hash}` : ""}${expiration ? `-ex${expiration}` : ""}.${extension}`,
2639
userId,
2740
prefix,
2841
name,
2942
hash,
3043
extension,
44+
expiration,
3145
};
3246
}

0 commit comments

Comments
 (0)