pg-mvc-service 2.0.7 → 2.0.9

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.
@@ -17,7 +17,10 @@ const pdf_lib_1 = require("pdf-lib");
17
17
  const sharp_1 = __importDefault(require("sharp"));
18
18
  const type_utils_n_daira_1 = require("type-utils-n-daira");
19
19
  class Base64Client {
20
- constructor() { }
20
+ constructor() {
21
+ this.PREFIX_JPEG_DATA = '/9j/';
22
+ this.PREFIX_PNG_DATA = 'iVBORw0KGgo';
23
+ }
21
24
  // public encode(text: string): string {
22
25
  // return Buffer.from(text).toString('base64');
23
26
  // }
@@ -151,7 +154,7 @@ class Base64Client {
151
154
  return Buffer.from(pdfBytes);
152
155
  });
153
156
  }
154
- static isJpeg(value) {
157
+ isJpeg(value) {
155
158
  if (type_utils_n_daira_1.ValidateStringUtil.isBase64(value) === false) {
156
159
  return false;
157
160
  }
@@ -167,7 +170,7 @@ class Base64Client {
167
170
  }
168
171
  return value.startsWith(this.PREFIX_JPEG_DATA);
169
172
  }
170
- static isPng(value) {
173
+ isPng(value) {
171
174
  if (type_utils_n_daira_1.ValidateStringUtil.isBase64(value) === false) {
172
175
  return false;
173
176
  }
@@ -183,7 +186,7 @@ class Base64Client {
183
186
  }
184
187
  return value.startsWith(this.PREFIX_PNG_DATA);
185
188
  }
186
- static tryConvertToPng(base64Value) {
189
+ tryConvertToPng(base64Value) {
187
190
  return __awaiter(this, void 0, void 0, function* () {
188
191
  if (type_utils_n_daira_1.ValidateStringUtil.isBase64(base64Value) === false) {
189
192
  return false;
@@ -206,7 +209,59 @@ class Base64Client {
206
209
  return false;
207
210
  });
208
211
  }
212
+ resizeImage(base64Data, toSize) {
213
+ return __awaiter(this, void 0, void 0, function* () {
214
+ if (type_utils_n_daira_1.ValidateStringUtil.isBase64(base64Data) === false) {
215
+ throw new Error("The specified data is not in base64 format");
216
+ }
217
+ const imageBuffer = Buffer.from(base64Data, 'base64');
218
+ const metadata = yield (0, sharp_1.default)(imageBuffer).metadata();
219
+ const { width, height, format } = metadata;
220
+ if (width === undefined || height === undefined) {
221
+ throw new Error("Failed to retrieve image dimensions");
222
+ }
223
+ let rate = 1;
224
+ if ('rate' in toSize) {
225
+ rate = toSize.rate;
226
+ }
227
+ else if ('w' in toSize && 'h' in toSize) {
228
+ const wRate = toSize.w / width;
229
+ const hRate = toSize.h / height;
230
+ rate = Math.max(wRate, hRate);
231
+ }
232
+ else if ('w' in toSize) {
233
+ rate = toSize.w / width;
234
+ }
235
+ else if ('h' in toSize) {
236
+ rate = toSize.h / height;
237
+ }
238
+ // 画像は1倍より大きくできないので
239
+ if (rate >= 1) {
240
+ return base64Data;
241
+ }
242
+ let resizedImage;
243
+ // フォーマットに応じて処理を分岐
244
+ if (format === 'png') {
245
+ resizedImage = yield (0, sharp_1.default)(imageBuffer)
246
+ .resize(width * rate, height * rate, {
247
+ fit: 'inside',
248
+ withoutEnlargement: true
249
+ })
250
+ .png({ quality: 90 })
251
+ .toBuffer();
252
+ }
253
+ else {
254
+ // JPEG、その他のフォーマット
255
+ resizedImage = yield (0, sharp_1.default)(imageBuffer)
256
+ .resize(width * rate, height * rate, {
257
+ fit: 'inside',
258
+ withoutEnlargement: true
259
+ })
260
+ .jpeg({ quality: 90 })
261
+ .toBuffer();
262
+ }
263
+ return resizedImage.toString('base64');
264
+ });
265
+ }
209
266
  }
210
267
  exports.Base64Client = Base64Client;
211
- Base64Client.PREFIX_JPEG_DATA = '/9j/';
212
- Base64Client.PREFIX_PNG_DATA = 'iVBORw0KGgo';
@@ -334,7 +334,7 @@ class TableModel {
334
334
  let message = this.errorMessages[type];
335
335
  const name = (column.alias === undefined || column.alias === '') ? columnName : column.alias;
336
336
  message = message.replace('{name}', name);
337
- if (message.includes("{length}") && (column.type === 'string' || column.type !== 'string[]')) {
337
+ if (message.includes("{length}") && (column.type === 'string' || column.type === 'string[]')) {
338
338
  message = message.replace('{length}', ((_a = column.length) !== null && _a !== void 0 ? _a : '未設定').toString());
339
339
  }
340
340
  throw new Exception_1.UnprocessableException(code, message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-mvc-service",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/n-daira/npm-pack_mvc-service#readme",
6
6
  "bugs": {
@@ -11,8 +11,8 @@ export type TPdf = 'application/pdf';
11
11
  export type TJson = 'application/json';
12
12
 
13
13
  export class Base64Client {
14
- public static readonly PREFIX_JPEG_DATA = '/9j/';
15
- public static readonly PREFIX_PNG_DATA = 'iVBORw0KGgo';
14
+ public readonly PREFIX_JPEG_DATA = '/9j/';
15
+ public readonly PREFIX_PNG_DATA = 'iVBORw0KGgo';
16
16
 
17
17
  constructor() { }
18
18
 
@@ -157,7 +157,7 @@ export class Base64Client {
157
157
  return Buffer.from(pdfBytes);
158
158
  }
159
159
 
160
- public static isJpeg(value: any): value is string {
160
+ public isJpeg(value: any): value is string {
161
161
  if (ValidateStringUtil.isBase64(value) === false) {
162
162
  return false
163
163
  }
@@ -178,7 +178,7 @@ export class Base64Client {
178
178
  return value.startsWith(this.PREFIX_JPEG_DATA);
179
179
  }
180
180
 
181
- public static isPng(value: any): value is string {
181
+ public isPng(value: any): value is string {
182
182
  if (ValidateStringUtil.isBase64(value) === false) {
183
183
  return false
184
184
  }
@@ -199,7 +199,7 @@ export class Base64Client {
199
199
  return value.startsWith(this.PREFIX_PNG_DATA);
200
200
  }
201
201
 
202
- public static async tryConvertToPng(base64Value: any): Promise<string | false> {
202
+ public async tryConvertToPng(base64Value: any): Promise<string | false> {
203
203
  if (ValidateStringUtil.isBase64(base64Value) === false) {
204
204
  return false;
205
205
  }
@@ -220,4 +220,59 @@ export class Base64Client {
220
220
 
221
221
  return false;
222
222
  }
223
+
224
+ public async resizeImage(base64Data: string, toSize: {w: number} | {h: number} | {w: number; h: number}| {rate: number}): Promise<string> {
225
+ if (ValidateStringUtil.isBase64(base64Data) === false) {
226
+ throw new Error("The specified data is not in base64 format");
227
+ }
228
+
229
+ const imageBuffer = Buffer.from(base64Data, 'base64');
230
+ const metadata = await sharp(imageBuffer).metadata();
231
+ const { width, height, format } = metadata;
232
+ if (width === undefined || height === undefined) {
233
+ throw new Error("Failed to retrieve image dimensions");
234
+ }
235
+
236
+ let rate = 1;
237
+ if ('rate' in toSize) {
238
+ rate = toSize.rate;
239
+ } else if ('w' in toSize && 'h' in toSize) {
240
+ const wRate = toSize.w / width;
241
+ const hRate = toSize.h / height;
242
+ rate = Math.max(wRate, hRate);
243
+ } else if ('w' in toSize) {
244
+ rate = toSize.w / width;
245
+ } else if ('h' in toSize) {
246
+ rate = toSize.h / height;
247
+ }
248
+
249
+ // 画像は1倍より大きくできないので
250
+ if (rate >= 1) {
251
+ return base64Data;
252
+ }
253
+
254
+ let resizedImage: Buffer;
255
+
256
+ // フォーマットに応じて処理を分岐
257
+ if (format === 'png') {
258
+ resizedImage = await sharp(imageBuffer)
259
+ .resize(width * rate, height * rate, {
260
+ fit: 'inside',
261
+ withoutEnlargement: true
262
+ })
263
+ .png({ quality: 90 })
264
+ .toBuffer();
265
+ } else {
266
+ // JPEG、その他のフォーマット
267
+ resizedImage = await sharp(imageBuffer)
268
+ .resize(width * rate, height * rate, {
269
+ fit: 'inside',
270
+ withoutEnlargement: true
271
+ })
272
+ .jpeg({ quality: 90 })
273
+ .toBuffer();
274
+ }
275
+
276
+ return resizedImage.toString('base64');
277
+ }
223
278
  }
@@ -393,7 +393,7 @@ export class TableModel {
393
393
 
394
394
  const name = (column.alias === undefined || column.alias === '') ? columnName : column.alias;
395
395
  message = message.replace('{name}', name);
396
- if (message.includes("{length}") && (column.type === 'string' || column.type !== 'string[]')) {
396
+ if (message.includes("{length}") && (column.type === 'string' || column.type === 'string[]')) {
397
397
  message = message.replace('{length}', (column.length ?? '未設定').toString());
398
398
  }
399
399