diff options
Diffstat (limited to 'Homework/cs5260/consumer/dao/keys/S3WidgetKeyGenerator.ts')
| -rw-r--r-- | Homework/cs5260/consumer/dao/keys/S3WidgetKeyGenerator.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Homework/cs5260/consumer/dao/keys/S3WidgetKeyGenerator.ts b/Homework/cs5260/consumer/dao/keys/S3WidgetKeyGenerator.ts new file mode 100644 index 0000000..2e4c254 --- /dev/null +++ b/Homework/cs5260/consumer/dao/keys/S3WidgetKeyGenerator.ts @@ -0,0 +1,43 @@ +import { WidgetT } from "@/shared/schema"; + +export class S3WidgetKeyGenerator { + private prefix: string = ""; + private owner: string = ""; + private widgetId: string = ""; + + public withPrefix(prefix: string): S3WidgetKeyGenerator { + this.prefix = prefix; + return this; + } + + public withOwner(owner: string): S3WidgetKeyGenerator { + this.owner = owner; + return this; + } + + public withWidgetId(widgetId: string): S3WidgetKeyGenerator { + this.widgetId = widgetId; + return this; + } + + public build(): string { + const parts = [ + this.prefix, + this.owner.replaceAll(" ", "-").toLowerCase(), + this.widgetId, + ]; + if (parts.some((part) => !part || !part.match(/^[a-zA-Z0-9!\-_.*'()]+$/))) { + throw new Error("Invalid Key Specification"); + } + return parts.join("/"); + } + + public fromWidget(widget: WidgetT) { + const keyBuilder = new S3WidgetKeyGenerator(); + return keyBuilder + .withPrefix(this.prefix) + .withOwner(widget.owner) + .withWidgetId(widget.id) + .build(); + } +} |
