pg-mvc-service 2.0.8 → 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.
@@ -209,5 +209,59 @@ class Base64Client {
209
209
  return false;
210
210
  });
211
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
+ }
212
266
  }
213
267
  exports.Base64Client = Base64Client;
@@ -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.8",
3
+ "version": "2.0.9",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/n-daira/npm-pack_mvc-service#readme",
6
6
  "bugs": {
@@ -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