-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: private files and asset delivery (#3799)
- Loading branch information
Showing
194 changed files
with
3,879 additions
and
1,644 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/api-file-manager-s3/src/assetDelivery/assetDeliveryConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { | ||
createAssetDelivery as createBaseAssetDelivery, | ||
createAssetDeliveryConfig | ||
} from "@webiny/api-file-manager"; | ||
import { S3 } from "@webiny/aws-sdk/client-s3"; | ||
import { S3AssetResolver } from "~/assetDelivery/s3/S3AssetResolver"; | ||
import { S3OutputStrategy } from "~/assetDelivery/s3/S3OutputStrategy"; | ||
import { SharpTransform } from "~/assetDelivery/s3/SharpTransform"; | ||
|
||
export type AssetDeliveryParams = Parameters<typeof createBaseAssetDelivery>[0] & { | ||
imageResizeWidths?: number[]; | ||
presignedUrlTtl?: number; | ||
}; | ||
|
||
export const assetDeliveryConfig = (params: AssetDeliveryParams) => { | ||
const bucket = process.env.S3_BUCKET as string; | ||
const region = process.env.AWS_REGION as string; | ||
|
||
const { | ||
presignedUrlTtl = 900, | ||
imageResizeWidths = [100, 300, 500, 750, 1000, 1500, 2500], | ||
...baseParams | ||
} = params; | ||
|
||
return [ | ||
// Base asset delivery | ||
createBaseAssetDelivery(baseParams), | ||
// S3 plugins | ||
createAssetDeliveryConfig(config => { | ||
const s3 = new S3({ region }); | ||
|
||
config.decorateAssetResolver(() => { | ||
// This resolver loads file information from the `.metadata` file. | ||
return new S3AssetResolver(s3, bucket); | ||
}); | ||
|
||
config.decorateAssetOutputStrategy(() => { | ||
return new S3OutputStrategy(s3, bucket, presignedUrlTtl); | ||
}); | ||
|
||
config.decorateAssetTransformationStrategy(() => { | ||
return new SharpTransform({ s3, bucket, imageResizeWidths }); | ||
}); | ||
}) | ||
]; | ||
}; |
14 changes: 14 additions & 0 deletions
14
packages/api-file-manager-s3/src/assetDelivery/createAssetDelivery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createAssetDeliveryPluginLoader } from "@webiny/api-file-manager"; | ||
import { PluginFactory } from "@webiny/plugins/types"; | ||
import type { AssetDeliveryParams } from "./assetDeliveryConfig"; | ||
|
||
export const createAssetDelivery = (params: AssetDeliveryParams): PluginFactory => { | ||
/** | ||
* We only want to load this plugin in the context of the Asset Delivery Lambda function. | ||
*/ | ||
return createAssetDeliveryPluginLoader(() => { | ||
return import(/* webpackChunkName: "s3AssetDelivery" */ "./assetDeliveryConfig").then( | ||
({ assetDeliveryConfig }) => assetDeliveryConfig(params) | ||
); | ||
}); | ||
}; |
44 changes: 44 additions & 0 deletions
44
packages/api-file-manager-s3/src/assetDelivery/s3/S3AssetMetadataReader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { S3 } from "@webiny/aws-sdk/client-s3"; | ||
|
||
interface AssetMetadata { | ||
id: string; | ||
tenant: string; | ||
locale: string; | ||
size: number; | ||
contentType: string; | ||
} | ||
|
||
export class S3AssetMetadataReader { | ||
private readonly s3: S3; | ||
private readonly bucket: string; | ||
|
||
constructor(s3: S3, bucket: string) { | ||
this.bucket = bucket; | ||
this.s3 = s3; | ||
} | ||
|
||
async getMetadata(key: string): Promise<AssetMetadata> { | ||
const metadataKey = `${key}.metadata`; | ||
|
||
console.log("Reading metadata", metadataKey); | ||
|
||
const { Body } = await this.s3.getObject({ | ||
Bucket: this.bucket, | ||
Key: metadataKey | ||
}); | ||
|
||
if (!Body) { | ||
throw Error(`Missing or corrupted ${metadataKey} file!`); | ||
} | ||
|
||
const metadata = JSON.parse(await Body.transformToString()); | ||
|
||
return { | ||
id: metadata.id, | ||
tenant: metadata.tenant, | ||
locale: metadata.locale, | ||
size: metadata.size, | ||
contentType: metadata.contentType | ||
}; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/api-file-manager-s3/src/assetDelivery/s3/S3AssetResolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { S3 } from "@webiny/aws-sdk/client-s3"; | ||
import { Asset, AssetRequest, AssetResolver } from "@webiny/api-file-manager"; | ||
import { S3AssetMetadataReader } from "./S3AssetMetadataReader"; | ||
import { S3ContentsReader } from "./S3ContentsReader"; | ||
|
||
export class S3AssetResolver implements AssetResolver { | ||
private readonly s3: S3; | ||
private readonly bucket: string; | ||
|
||
constructor(s3: S3, bucket: string) { | ||
this.s3 = s3; | ||
this.bucket = bucket; | ||
} | ||
|
||
async resolve(request: AssetRequest): Promise<Asset | undefined> { | ||
try { | ||
const metadataReader = new S3AssetMetadataReader(this.s3, this.bucket); | ||
const metadata = await metadataReader.getMetadata(request.getKey()); | ||
|
||
const asset = new Asset({ | ||
id: metadata.id, | ||
tenant: metadata.tenant, | ||
locale: metadata.locale, | ||
size: metadata.size, | ||
contentType: metadata.contentType, | ||
key: request.getKey() | ||
}); | ||
|
||
asset.setContentsReader(new S3ContentsReader(this.s3, this.bucket)); | ||
|
||
return asset; | ||
} catch (error) { | ||
console.error(error); | ||
return undefined; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/api-file-manager-s3/src/assetDelivery/s3/S3ContentsReader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { S3 } from "@webiny/aws-sdk/client-s3"; | ||
import { Asset, AssetContentsReader } from "@webiny/api-file-manager"; | ||
|
||
export class S3ContentsReader implements AssetContentsReader { | ||
private s3: S3; | ||
private readonly bucket: string; | ||
|
||
constructor(s3: S3, bucket: string) { | ||
this.s3 = s3; | ||
this.bucket = bucket; | ||
} | ||
|
||
async read(asset: Asset): Promise<Buffer> { | ||
const { Body } = await this.s3.getObject({ | ||
Bucket: this.bucket, | ||
Key: asset.getKey() | ||
}); | ||
|
||
if (!Body) { | ||
throw Error(`Unable to read ${asset.getKey()}!`); | ||
} | ||
|
||
return Buffer.from(await Body.transformToByteArray()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/api-file-manager-s3/src/assetDelivery/s3/S3ErrorAssetReply.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { AssetReply } from "@webiny/api-file-manager"; | ||
|
||
export class S3ErrorAssetReply extends AssetReply { | ||
constructor(message: string) { | ||
super({ | ||
code: 400, | ||
body: () => ({ error: message }) | ||
}); | ||
} | ||
} |
Oops, something went wrong.