@vendure/asset-server-plugin 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +7 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +17 -0
- package/lib/index.js.map +1 -0
- package/lib/src/common.d.ts +3 -0
- package/lib/src/common.js +22 -0
- package/lib/src/common.js.map +1 -0
- package/lib/src/constants.d.ts +1 -0
- package/lib/src/constants.js +5 -0
- package/lib/src/constants.js.map +1 -0
- package/lib/src/default-asset-storage-strategy-factory.d.ts +6 -0
- package/lib/src/default-asset-storage-strategy-factory.js +22 -0
- package/lib/src/default-asset-storage-strategy-factory.js.map +1 -0
- package/lib/src/file-icon.png +0 -0
- package/lib/src/hashed-asset-naming-strategy.d.ts +21 -0
- package/lib/src/hashed-asset-naming-strategy.js +39 -0
- package/lib/src/hashed-asset-naming-strategy.js.map +1 -0
- package/lib/src/local-asset-storage-strategy.d.ts +25 -0
- package/lib/src/local-asset-storage-strategy.js +68 -0
- package/lib/src/local-asset-storage-strategy.js.map +1 -0
- package/lib/src/plugin.d.ts +141 -0
- package/lib/src/plugin.js +317 -0
- package/lib/src/plugin.js.map +1 -0
- package/lib/src/s3-asset-storage-strategy.d.ts +118 -0
- package/lib/src/s3-asset-storage-strategy.js +222 -0
- package/lib/src/s3-asset-storage-strategy.js.map +1 -0
- package/lib/src/sharp-asset-preview-strategy.d.ts +11 -0
- package/lib/src/sharp-asset-preview-strategy.js +64 -0
- package/lib/src/sharp-asset-preview-strategy.js.map +1 -0
- package/lib/src/transform-image.d.ts +24 -0
- package/lib/src/transform-image.js +102 -0
- package/lib/src/transform-image.js.map +1 -0
- package/lib/src/types.d.ts +98 -0
- package/lib/src/types.js +3 -0
- package/lib/src/types.js.map +1 -0
- package/package.json +41 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"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AK,MAAM;gBACP,OAAO,WAAW,CAAC;YACvB,KAAK,MAAM;gBACP,OAAO,eAAe,CAAC;YAC3B,KAAK,OAAO;gBACR,OAAO,YAAY,CAAC;YACxB,KAAK,OAAO;gBACR,OAAO,YAAY,CAAC;SAC3B;IACL,CAAC;CACJ,CAAA;AAjMY,iBAAiB;IAJ7B,oBAAa,CAAC;QACX,OAAO,EAAE,CAAC,yBAAkB,CAAC;QAC7B,aAAa,EAAE,MAAM,CAAC,EAAE,CAAC,mBAAiB,CAAC,SAAS,CAAC,MAAM,CAAC;KAC/D,CAAC;qCAqCsC,qBAAc;GApCzC,iBAAiB,CAiM7B;AAjMY,8CAAiB"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AssetStorageStrategy } from '@vendure/core';
|
|
3
|
+
import { Request } from 'express';
|
|
4
|
+
import { Stream } from 'stream';
|
|
5
|
+
import { AssetServerOptions } from './types';
|
|
6
|
+
export declare type S3Credentials = {
|
|
7
|
+
accessKeyId: string;
|
|
8
|
+
secretAccessKey: string;
|
|
9
|
+
};
|
|
10
|
+
export declare type S3CredentialsProfile = {
|
|
11
|
+
profile: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* @description
|
|
15
|
+
* Configuration for connecting to AWS S3.
|
|
16
|
+
*
|
|
17
|
+
* @docsCategory asset-server-plugin
|
|
18
|
+
* @docsPage S3AssetStorageStrategy
|
|
19
|
+
*/
|
|
20
|
+
export interface S3Config {
|
|
21
|
+
/**
|
|
22
|
+
* @description
|
|
23
|
+
* The credentials used to access your s3 account. You can supply either the access key ID & secret,
|
|
24
|
+
* or you can make use of a
|
|
25
|
+
* [shared credentials file](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html)
|
|
26
|
+
* and supply the profile name (e.g. `'default'`).
|
|
27
|
+
*/
|
|
28
|
+
credentials?: S3Credentials | S3CredentialsProfile;
|
|
29
|
+
/**
|
|
30
|
+
* @description
|
|
31
|
+
* The S3 bucket in which to store the assets. If it does not exist, it will be created on startup.
|
|
32
|
+
*/
|
|
33
|
+
bucket: string;
|
|
34
|
+
/**
|
|
35
|
+
* @description
|
|
36
|
+
* Configuration object passed directly to the AWS SDK.
|
|
37
|
+
* S3.Types.ClientConfiguration can be used after importing aws-sdk.
|
|
38
|
+
* Using type `any` in order to avoid the need to include `aws-sdk` dependency in general.
|
|
39
|
+
*/
|
|
40
|
+
nativeS3Configuration?: any;
|
|
41
|
+
/**
|
|
42
|
+
* @description
|
|
43
|
+
* Configuration object passed directly to the AWS SDK.
|
|
44
|
+
* ManagedUpload.ManagedUploadOptions can be used after importing aws-sdk.
|
|
45
|
+
* Using type `any` in order to avoid the need to include `aws-sdk` dependency in general.
|
|
46
|
+
*/
|
|
47
|
+
nativeS3UploadConfiguration?: any;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @description
|
|
51
|
+
* Returns a configured instance of the {@link S3AssetStorageStrategy} which can then be passed to the {@link AssetServerOptions}
|
|
52
|
+
* `storageStrategyFactory` property.
|
|
53
|
+
*
|
|
54
|
+
* Before using this strategy, make sure you have the `aws-sdk` package installed:
|
|
55
|
+
*
|
|
56
|
+
* ```sh
|
|
57
|
+
* npm install aws-sdk
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```TypeScript
|
|
62
|
+
* plugins: [
|
|
63
|
+
* AssetServerPlugin.init({
|
|
64
|
+
* route: 'assets',
|
|
65
|
+
* assetUploadDir: path.join(__dirname, 'assets'),
|
|
66
|
+
* port: 5002,
|
|
67
|
+
* namingStrategy: new DefaultAssetNamingStrategy(),
|
|
68
|
+
* storageStrategyFactory: configureS3AssetStorage({
|
|
69
|
+
* bucket: 'my-s3-bucket',
|
|
70
|
+
* credentials: {
|
|
71
|
+
* accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
72
|
+
* secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
73
|
+
* },
|
|
74
|
+
* }),
|
|
75
|
+
* }),
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @docsCategory asset-server-plugin
|
|
79
|
+
* @docsPage S3AssetStorageStrategy
|
|
80
|
+
*/
|
|
81
|
+
export declare function configureS3AssetStorage(s3Config: S3Config): (options: AssetServerOptions) => S3AssetStorageStrategy;
|
|
82
|
+
/**
|
|
83
|
+
* @description
|
|
84
|
+
* An {@link AssetStorageStrategy} which uses [Amazon S3](https://aws.amazon.com/s3/) object storage service.
|
|
85
|
+
* To us this strategy you must first have access to an AWS account.
|
|
86
|
+
* See their [getting started guide](https://aws.amazon.com/s3/getting-started/?nc=sn&loc=5) for how to get set up.
|
|
87
|
+
*
|
|
88
|
+
* Before using this strategy, make sure you have the `aws-sdk` package installed:
|
|
89
|
+
*
|
|
90
|
+
* ```sh
|
|
91
|
+
* npm install aws-sdk
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* **Note:** Rather than instantiating this manually, use the {@link configureS3AssetStorage} function.
|
|
95
|
+
*
|
|
96
|
+
* @docsCategory asset-server-plugin
|
|
97
|
+
* @docsPage S3AssetStorageStrategy
|
|
98
|
+
* @docsWeight 0
|
|
99
|
+
*/
|
|
100
|
+
export declare class S3AssetStorageStrategy implements AssetStorageStrategy {
|
|
101
|
+
private s3Config;
|
|
102
|
+
readonly toAbsoluteUrl: (request: Request, identifier: string) => string;
|
|
103
|
+
private AWS;
|
|
104
|
+
private s3;
|
|
105
|
+
constructor(s3Config: S3Config, toAbsoluteUrl: (request: Request, identifier: string) => string);
|
|
106
|
+
init(): Promise<void>;
|
|
107
|
+
destroy?: (() => void | Promise<void>) | undefined;
|
|
108
|
+
writeFileFromBuffer(fileName: string, data: Buffer): Promise<string>;
|
|
109
|
+
writeFileFromStream(fileName: string, data: Stream): Promise<string>;
|
|
110
|
+
readFileToBuffer(identifier: string): Promise<Buffer>;
|
|
111
|
+
readFileToStream(identifier: string): Promise<Stream>;
|
|
112
|
+
deleteFile(identifier: string): Promise<void>;
|
|
113
|
+
fileExists(fileName: string): Promise<boolean>;
|
|
114
|
+
private getObjectParams;
|
|
115
|
+
private getS3Credentials;
|
|
116
|
+
private ensureBucket;
|
|
117
|
+
private isCredentialsProfile;
|
|
118
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.S3AssetStorageStrategy = exports.configureS3AssetStorage = void 0;
|
|
23
|
+
const core_1 = require("@vendure/core");
|
|
24
|
+
const path = __importStar(require("path"));
|
|
25
|
+
const stream_1 = require("stream");
|
|
26
|
+
const common_1 = require("./common");
|
|
27
|
+
const constants_1 = require("./constants");
|
|
28
|
+
/**
|
|
29
|
+
* @description
|
|
30
|
+
* Returns a configured instance of the {@link S3AssetStorageStrategy} which can then be passed to the {@link AssetServerOptions}
|
|
31
|
+
* `storageStrategyFactory` property.
|
|
32
|
+
*
|
|
33
|
+
* Before using this strategy, make sure you have the `aws-sdk` package installed:
|
|
34
|
+
*
|
|
35
|
+
* ```sh
|
|
36
|
+
* npm install aws-sdk
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```TypeScript
|
|
41
|
+
* plugins: [
|
|
42
|
+
* AssetServerPlugin.init({
|
|
43
|
+
* route: 'assets',
|
|
44
|
+
* assetUploadDir: path.join(__dirname, 'assets'),
|
|
45
|
+
* port: 5002,
|
|
46
|
+
* namingStrategy: new DefaultAssetNamingStrategy(),
|
|
47
|
+
* storageStrategyFactory: configureS3AssetStorage({
|
|
48
|
+
* bucket: 'my-s3-bucket',
|
|
49
|
+
* credentials: {
|
|
50
|
+
* accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
51
|
+
* secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
|
|
52
|
+
* },
|
|
53
|
+
* }),
|
|
54
|
+
* }),
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @docsCategory asset-server-plugin
|
|
58
|
+
* @docsPage S3AssetStorageStrategy
|
|
59
|
+
*/
|
|
60
|
+
function configureS3AssetStorage(s3Config) {
|
|
61
|
+
return (options) => {
|
|
62
|
+
const { assetUrlPrefix, route } = options;
|
|
63
|
+
const prefixFn = common_1.getAssetUrlPrefixFn(options);
|
|
64
|
+
const toAbsoluteUrlFn = (request, identifier) => {
|
|
65
|
+
if (!identifier) {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
const prefix = prefixFn(request, identifier);
|
|
69
|
+
return identifier.startsWith(prefix) ? identifier : `${prefix}${identifier}`;
|
|
70
|
+
};
|
|
71
|
+
return new S3AssetStorageStrategy(s3Config, toAbsoluteUrlFn);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
exports.configureS3AssetStorage = configureS3AssetStorage;
|
|
75
|
+
/**
|
|
76
|
+
* @description
|
|
77
|
+
* An {@link AssetStorageStrategy} which uses [Amazon S3](https://aws.amazon.com/s3/) object storage service.
|
|
78
|
+
* To us this strategy you must first have access to an AWS account.
|
|
79
|
+
* See their [getting started guide](https://aws.amazon.com/s3/getting-started/?nc=sn&loc=5) for how to get set up.
|
|
80
|
+
*
|
|
81
|
+
* Before using this strategy, make sure you have the `aws-sdk` package installed:
|
|
82
|
+
*
|
|
83
|
+
* ```sh
|
|
84
|
+
* npm install aws-sdk
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* **Note:** Rather than instantiating this manually, use the {@link configureS3AssetStorage} function.
|
|
88
|
+
*
|
|
89
|
+
* @docsCategory asset-server-plugin
|
|
90
|
+
* @docsPage S3AssetStorageStrategy
|
|
91
|
+
* @docsWeight 0
|
|
92
|
+
*/
|
|
93
|
+
class S3AssetStorageStrategy {
|
|
94
|
+
constructor(s3Config, toAbsoluteUrl) {
|
|
95
|
+
this.s3Config = s3Config;
|
|
96
|
+
this.toAbsoluteUrl = toAbsoluteUrl;
|
|
97
|
+
}
|
|
98
|
+
async init() {
|
|
99
|
+
try {
|
|
100
|
+
this.AWS = await Promise.resolve().then(() => __importStar(require('aws-sdk')));
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
core_1.Logger.error(`Could not find the "aws-sdk" package. Make sure it is installed`, constants_1.loggerCtx, e.stack);
|
|
104
|
+
}
|
|
105
|
+
const config = Object.assign({ credentials: this.getS3Credentials() }, this.s3Config.nativeS3Configuration);
|
|
106
|
+
this.s3 = new this.AWS.S3(config);
|
|
107
|
+
await this.ensureBucket(this.s3Config.bucket);
|
|
108
|
+
}
|
|
109
|
+
async writeFileFromBuffer(fileName, data) {
|
|
110
|
+
const result = await this.s3
|
|
111
|
+
.upload({
|
|
112
|
+
Bucket: this.s3Config.bucket,
|
|
113
|
+
Key: fileName,
|
|
114
|
+
Body: data,
|
|
115
|
+
}, this.s3Config.nativeS3UploadConfiguration)
|
|
116
|
+
.promise();
|
|
117
|
+
return result.Key;
|
|
118
|
+
}
|
|
119
|
+
async writeFileFromStream(fileName, data) {
|
|
120
|
+
const result = await this.s3
|
|
121
|
+
.upload({
|
|
122
|
+
Bucket: this.s3Config.bucket,
|
|
123
|
+
Key: fileName,
|
|
124
|
+
Body: data,
|
|
125
|
+
}, this.s3Config.nativeS3UploadConfiguration)
|
|
126
|
+
.promise();
|
|
127
|
+
return result.Key;
|
|
128
|
+
}
|
|
129
|
+
async readFileToBuffer(identifier) {
|
|
130
|
+
const result = await this.s3.getObject(this.getObjectParams(identifier)).promise();
|
|
131
|
+
const body = result.Body;
|
|
132
|
+
if (!body) {
|
|
133
|
+
core_1.Logger.error(`Got undefined Body for ${identifier}`, constants_1.loggerCtx);
|
|
134
|
+
return Buffer.from('');
|
|
135
|
+
}
|
|
136
|
+
if (body instanceof Buffer) {
|
|
137
|
+
return body;
|
|
138
|
+
}
|
|
139
|
+
if (body instanceof Uint8Array || typeof body === 'string') {
|
|
140
|
+
return Buffer.from(body);
|
|
141
|
+
}
|
|
142
|
+
if (body instanceof stream_1.Readable) {
|
|
143
|
+
return new Promise((resolve, reject) => {
|
|
144
|
+
const buf = [];
|
|
145
|
+
body.on('data', data => buf.push(data));
|
|
146
|
+
body.on('error', err => reject(err));
|
|
147
|
+
body.on('end', () => {
|
|
148
|
+
const intArray = Uint8Array.from(buf);
|
|
149
|
+
resolve(Buffer.concat([intArray]));
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return Buffer.from(body);
|
|
154
|
+
}
|
|
155
|
+
async readFileToStream(identifier) {
|
|
156
|
+
const result = await this.s3.getObject(this.getObjectParams(identifier)).promise();
|
|
157
|
+
const body = result.Body;
|
|
158
|
+
if (!(body instanceof stream_1.Stream)) {
|
|
159
|
+
const readable = new stream_1.Readable();
|
|
160
|
+
readable._read = () => {
|
|
161
|
+
/* noop */
|
|
162
|
+
};
|
|
163
|
+
readable.push(body);
|
|
164
|
+
readable.push(null);
|
|
165
|
+
return readable;
|
|
166
|
+
}
|
|
167
|
+
return body;
|
|
168
|
+
}
|
|
169
|
+
async deleteFile(identifier) {
|
|
170
|
+
await this.s3.deleteObject(this.getObjectParams(identifier)).promise();
|
|
171
|
+
}
|
|
172
|
+
async fileExists(fileName) {
|
|
173
|
+
try {
|
|
174
|
+
await this.s3.headObject(this.getObjectParams(fileName)).promise();
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
catch (e) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
getObjectParams(identifier) {
|
|
182
|
+
return {
|
|
183
|
+
Bucket: this.s3Config.bucket,
|
|
184
|
+
Key: path.join(identifier.replace(/^\//, '')),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
getS3Credentials() {
|
|
188
|
+
const { credentials } = this.s3Config;
|
|
189
|
+
if (credentials == null) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
else if (this.isCredentialsProfile(credentials)) {
|
|
193
|
+
return new this.AWS.SharedIniFileCredentials(credentials);
|
|
194
|
+
}
|
|
195
|
+
return new this.AWS.Credentials(credentials);
|
|
196
|
+
}
|
|
197
|
+
async ensureBucket(bucket) {
|
|
198
|
+
let bucketExists = false;
|
|
199
|
+
try {
|
|
200
|
+
await this.s3.headBucket({ Bucket: this.s3Config.bucket }).promise();
|
|
201
|
+
bucketExists = true;
|
|
202
|
+
core_1.Logger.verbose(`Found S3 bucket "${bucket}"`, constants_1.loggerCtx);
|
|
203
|
+
}
|
|
204
|
+
catch (e) {
|
|
205
|
+
core_1.Logger.verbose(`Could not find bucket "${bucket}". Attempting to create...`);
|
|
206
|
+
}
|
|
207
|
+
if (!bucketExists) {
|
|
208
|
+
try {
|
|
209
|
+
await this.s3.createBucket({ Bucket: bucket, ACL: 'private' }).promise();
|
|
210
|
+
core_1.Logger.verbose(`Created S3 bucket "${bucket}"`, constants_1.loggerCtx);
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
core_1.Logger.error(`Could not find nor create the S3 bucket "${bucket}"`, constants_1.loggerCtx, e.stack);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
isCredentialsProfile(credentials) {
|
|
218
|
+
return credentials.hasOwnProperty('profile');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
exports.S3AssetStorageStrategy = S3AssetStorageStrategy;
|
|
222
|
+
//# sourceMappingURL=s3-asset-storage-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3-asset-storage-strategy.js","sourceRoot":"","sources":["../../src/s3-asset-storage-strategy.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,wCAA6D;AAE7D,2CAA6B;AAC7B,mCAA0C;AAE1C,qCAA+C;AAC/C,2CAAwC;AAiDxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,SAAgB,uBAAuB,CAAC,QAAkB;IACtD,OAAO,CAAC,OAA2B,EAAE,EAAE;QACnC,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC;QAC1C,MAAM,QAAQ,GAAG,4BAAmB,CAAC,OAAO,CAAC,CAAC;QAC9C,MAAM,eAAe,GAAG,CAAC,OAAgB,EAAE,UAAkB,EAAU,EAAE;YACrE,IAAI,CAAC,UAAU,EAAE;gBACb,OAAO,EAAE,CAAC;aACb;YACD,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;YAC7C,OAAO,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,UAAU,EAAE,CAAC;QACjF,CAAC,CAAC;QACF,OAAO,IAAI,sBAAsB,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC;IACjE,CAAC,CAAC;AACN,CAAC;AAb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CAAC,KAAK,CAAC,CAAC;aAC3F;SACJ;IACL,CAAC;IAEO,oBAAoB,CACxB,WAAiD;QAEjD,OAAO,WAAW,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC;IACjD,CAAC;CACJ;AAzJD,wDAyJC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AssetPreviewStrategy, RequestContext } from '@vendure/core';
|
|
3
|
+
export declare class SharpAssetPreviewStrategy implements AssetPreviewStrategy {
|
|
4
|
+
private config;
|
|
5
|
+
constructor(config: {
|
|
6
|
+
maxHeight: number;
|
|
7
|
+
maxWidth: number;
|
|
8
|
+
});
|
|
9
|
+
generatePreviewImage(ctx: RequestContext, mimeType: string, data: Buffer): Promise<Buffer>;
|
|
10
|
+
private generateMimeTypeOverlay;
|
|
11
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SharpAssetPreviewStrategy = void 0;
|
|
7
|
+
const generated_types_1 = require("@vendure/common/lib/generated-types");
|
|
8
|
+
const core_1 = require("@vendure/core");
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const sharp_1 = __importDefault(require("sharp"));
|
|
11
|
+
class SharpAssetPreviewStrategy {
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.config = config;
|
|
14
|
+
}
|
|
15
|
+
async generatePreviewImage(ctx, mimeType, data) {
|
|
16
|
+
const assetType = core_1.getAssetType(mimeType);
|
|
17
|
+
const { maxWidth, maxHeight } = this.config;
|
|
18
|
+
if (assetType === generated_types_1.AssetType.IMAGE) {
|
|
19
|
+
const image = sharp_1.default(data);
|
|
20
|
+
const metadata = await image.metadata();
|
|
21
|
+
const width = metadata.width || 0;
|
|
22
|
+
const height = metadata.height || 0;
|
|
23
|
+
if (maxWidth < width || maxHeight < height) {
|
|
24
|
+
return image.resize(maxWidth, maxHeight, { fit: 'inside' }).toBuffer();
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
if (mimeType === 'image/svg+xml') {
|
|
28
|
+
// Convert the SVG to a raster for the preview
|
|
29
|
+
return image.toBuffer();
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return sharp_1.default(path_1.default.join(__dirname, 'file-icon.png'))
|
|
38
|
+
.resize(800, 800, { fit: 'outside' })
|
|
39
|
+
.composite([
|
|
40
|
+
{
|
|
41
|
+
input: this.generateMimeTypeOverlay(mimeType),
|
|
42
|
+
gravity: sharp_1.default.gravity.center,
|
|
43
|
+
},
|
|
44
|
+
])
|
|
45
|
+
.toBuffer();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
generateMimeTypeOverlay(mimeType) {
|
|
49
|
+
return Buffer.from(`
|
|
50
|
+
<svg xmlns="http://www.w3.org/2000/svg" height="150" width="800">
|
|
51
|
+
<style>
|
|
52
|
+
text {
|
|
53
|
+
font-size: 64px;
|
|
54
|
+
font-family: Arial, sans-serif;
|
|
55
|
+
fill: #666;
|
|
56
|
+
}
|
|
57
|
+
</style>
|
|
58
|
+
|
|
59
|
+
<text x="400" y="110" text-anchor="middle" width="800">${mimeType}</text>
|
|
60
|
+
</svg>`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.SharpAssetPreviewStrategy = SharpAssetPreviewStrategy;
|
|
64
|
+
//# sourceMappingURL=sharp-asset-preview-strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sharp-asset-preview-strategy.js","sourceRoot":"","sources":["../../src/sharp-asset-preview-strategy.ts"],"names":[],"mappings":";;;;;;AAAA,yEAAgE;AAChE,w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AAK,CAAC,OAAO,CAAC,MAAM;iBAChC;aACJ,CAAC;iBACD,QAAQ,EAAE,CAAC;SACnB;IACL,CAAC;IAEO,uBAAuB,CAAC,QAAgB;QAC5C,OAAO,MAAM,CAAC,IAAI,CAAC;;;;;;;;;;wEAU6C,QAAQ;mBAC7D,CAAC,CAAC;IACjB,CAAC;CACJ;AAtDD,8DAsDC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import sharp, { Region } from 'sharp';
|
|
3
|
+
import { ImageTransformPreset } from './types';
|
|
4
|
+
export declare type Dimensions = {
|
|
5
|
+
w: number;
|
|
6
|
+
h: number;
|
|
7
|
+
};
|
|
8
|
+
export declare type Point = {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Applies transforms to the given image according to the query params passed.
|
|
14
|
+
*/
|
|
15
|
+
export declare function transformImage(originalImage: Buffer, queryParams: Record<string, string>, presets: ImageTransformPreset[]): Promise<sharp.Sharp>;
|
|
16
|
+
/**
|
|
17
|
+
* Resize an image but keep it centered on the focal point.
|
|
18
|
+
* Based on the method outlined in https://github.com/lovell/sharp/issues/1198#issuecomment-384591756
|
|
19
|
+
*/
|
|
20
|
+
export declare function resizeToFocalPoint(original: Dimensions, target: Dimensions, focalPoint: Point): {
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
region: Region;
|
|
24
|
+
};
|