pg-mvc-service 2.0.29 → 2.0.31

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.
@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Base64Client = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
16
17
  const pdf_lib_1 = require("pdf-lib");
17
18
  const sharp_1 = __importDefault(require("sharp"));
18
19
  const type_utils_n_daira_1 = require("type-utils-n-daira");
@@ -272,5 +273,20 @@ class Base64Client {
272
273
  return resizedImage.toString('base64');
273
274
  });
274
275
  }
276
+ fetchImageAsBase64(imageUrl) {
277
+ return __awaiter(this, void 0, void 0, function* () {
278
+ const res = yield axios_1.default.get(imageUrl);
279
+ // Content-Typeをチェック
280
+ const contentType = res.headers['content-type'];
281
+ if (!(contentType === null || contentType === void 0 ? void 0 : contentType.startsWith('image/'))) {
282
+ throw new Error(`Invalid content type: ${contentType}. Expected image/*`);
283
+ }
284
+ // ArrayBufferをBufferに変換してBase64にエンコード
285
+ const buffer = Buffer.from(res.data);
286
+ const base64 = buffer.toString('base64');
287
+ const mimeType = this.getMimeType(base64);
288
+ return `data:${mimeType};base64,${base64}`;
289
+ });
290
+ }
275
291
  }
276
292
  exports.Base64Client = Base64Client;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-mvc-service",
3
- "version": "2.0.29",
3
+ "version": "2.0.31",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/n-daira/npm-pack_mvc-service#readme",
6
6
  "bugs": {
@@ -1,3 +1,4 @@
1
+ import axios from 'axios';
1
2
  import { PDFDocument } from 'pdf-lib';
2
3
  import sharp from 'sharp';
3
4
  import { ValidateStringUtil } from 'type-utils-n-daira';
@@ -285,4 +286,22 @@ export class Base64Client {
285
286
 
286
287
  return resizedImage.toString('base64');
287
288
  }
289
+
290
+ public async fetchImageAsBase64(imageUrl: string): Promise<string> {
291
+ const res = await axios.get(imageUrl);
292
+
293
+ // Content-Typeをチェック
294
+ const contentType = res.headers['content-type'];
295
+ if (!contentType?.startsWith('image/')) {
296
+ throw new Error(`Invalid content type: ${contentType}. Expected image/*`);
297
+ }
298
+
299
+ // ArrayBufferをBufferに変換してBase64にエンコード
300
+ const buffer = Buffer.from(res.data);
301
+ const base64 = buffer.toString('base64');
302
+
303
+ const mimeType = this.getMimeType(base64);
304
+
305
+ return `data:${mimeType};base64,${base64}`;
306
+ }
288
307
  }