11// BlobStorage
22import {
3- BlobServiceClient ,
4- StorageSharedKeyCredential
3+ BlobServiceClient ,
4+ StorageSharedKeyCredential
55} from '@azure/storage-blob' ;
6+ import sharp from 'sharp' ; // 画像処理ライブラリを追加
67
78// ファイルを取得する
89// output: base64
910export const getBase64File = async ( file_path : string ) : Promise < string > => {
10- return new Promise ( async ( resolve , reject ) => {
11- const sharedKeyCredential = new StorageSharedKeyCredential (
12- process . env . AZURE_STORAGE_ACCOUNT_NAME ! ,
13- process . env . AZURE_STORAGE_ACCOUNT_ACCESS_KEY !
14- ) ;
15- const blobServiceClient = new BlobServiceClient (
16- `https://${ process . env . AZURE_STORAGE_ACCOUNT_NAME } .blob.core.windows.net` ,
17- sharedKeyCredential
18- ) ;
19- const containerClient = blobServiceClient . getContainerClient ( process . env . AZURE_STORAGE_CONTAINER_NAME ! ) ;
20- const blobClient = containerClient . getBlobClient ( file_path ) ;
21-
22- //blobからデータをダウンロード
23- const downloadResponse = await blobClient . download ( 0 ) ;
24-
25- //データを文字列に
26- let encodedData = '' ;
27- if ( downloadResponse . readableStreamBody ) {
28- const downloaded = await streamToBuffer ( downloadResponse . readableStreamBody ) ;
29- const encodedData = downloaded . toString ( 'base64' ) ;
30- resolve ( encodedData ) ;
31- } else {
32- reject ( 'readableStreamBody is undefined' ) ;
33- }
34-
35- resolve ( encodedData ) ;
36-
37- } ) ;
11+ return new Promise ( async ( resolve , reject ) => {
12+ const sharedKeyCredential = new StorageSharedKeyCredential (
13+ process . env . AZURE_STORAGE_ACCOUNT_NAME ! ,
14+ process . env . AZURE_STORAGE_ACCOUNT_ACCESS_KEY !
15+ ) ;
16+ const blobServiceClient = new BlobServiceClient (
17+ `https://${ process . env . AZURE_STORAGE_ACCOUNT_NAME } .blob.core.windows.net` ,
18+ sharedKeyCredential
19+ ) ;
20+ const containerClient = blobServiceClient . getContainerClient ( process . env . AZURE_STORAGE_CONTAINER_NAME ! ) ;
21+ const blobClient = containerClient . getBlobClient ( file_path ) ;
22+
23+ // blobからデータをダウンロード
24+ const downloadResponse = await blobClient . download ( 0 ) ;
25+
26+ // データを文字列に
27+ if ( downloadResponse . readableStreamBody ) {
28+ const downloaded = await streamToBuffer ( downloadResponse . readableStreamBody ) ;
29+
30+ // 画像を縮小
31+ const resizedBuffer = await resizeImage ( downloaded ) ;
32+
33+ const mimeType = getMimeType ( file_path ) ; // MIMEタイプを取得
34+ const encodedData = `data:${ mimeType } ;base64,${ resizedBuffer . toString ( 'base64' ) } ` ;
35+ resolve ( encodedData ) ;
36+ } else {
37+ reject ( 'readableStreamBody is undefined' ) ;
38+ }
39+ } ) ;
3840} ;
3941
42+ // BlobのURLを取得する
43+ export const getBlobUrl = async ( file_path : string ) : Promise < string > => {
44+ const sharedKeyCredential = new StorageSharedKeyCredential (
45+ process . env . AZURE_STORAGE_ACCOUNT_NAME ! ,
46+ process . env . AZURE_STORAGE_ACCOUNT_ACCESS_KEY !
47+ ) ;
48+ const blobServiceClient = new BlobServiceClient (
49+ `https://${ process . env . AZURE_STORAGE_ACCOUNT_NAME } .blob.core.windows.net` ,
50+ sharedKeyCredential
51+ ) ;
52+ const containerClient = blobServiceClient . getContainerClient ( process . env . AZURE_STORAGE_CONTAINER_NAME ! ) ;
53+ const blobClient = containerClient . getBlobClient ( file_path ) ;
54+
55+ // BlobのURLを生成
56+ return blobClient . url ;
57+ } ;
58+
59+ // 画像を縮小する関数
60+ async function resizeImage ( buffer : Buffer ) : Promise < Buffer > {
61+ try {
62+ return await sharp ( buffer )
63+ . resize ( { width : 800 } ) // 幅を800pxに縮小(必要に応じて調整)
64+ . toBuffer ( ) ;
65+ } catch ( error ) {
66+ console . error ( '画像の縮小に失敗しました:' , error ) ;
67+ throw error ;
68+ }
69+ }
70+
71+ // ファイルパスからMIMEタイプを推測
72+ function getMimeType ( filePath : string ) : string {
73+ const extension = filePath . split ( '.' ) . pop ( ) ?. toLowerCase ( ) ;
74+ switch ( extension ) {
75+ case 'jpg' :
76+ case 'jpeg' :
77+ return 'image/jpeg' ;
78+ case 'png' :
79+ return 'image/png' ;
80+ case 'gif' :
81+ return 'image/gif' ;
82+ case 'txt' :
83+ return 'text/plain' ;
84+ case 'pdf' :
85+ return 'application/pdf' ;
86+ default :
87+ return 'application/octet-stream' ;
88+ }
89+ }
90+
4091async function streamToBuffer ( readableStream : NodeJS . ReadableStream ) : Promise < Buffer > {
41- return new Promise ( ( resolve , reject ) => {
42- const chunks : Buffer [ ] = [ ] ;
43- readableStream . on ( 'data' , ( data ) => {
44- chunks . push ( data instanceof Buffer ? data : Buffer . from ( data ) ) ;
45- } ) ;
46- readableStream . on ( 'end' , ( ) => {
47- resolve ( Buffer . concat ( chunks ) ) ;
48- } ) ;
49- readableStream . on ( 'error' , reject ) ;
50- } ) ;
92+ return new Promise ( ( resolve , reject ) => {
93+ const chunks : Buffer [ ] = [ ] ;
94+ readableStream . on ( 'data' , ( data ) => {
95+ chunks . push ( data instanceof Buffer ? data : Buffer . from ( data ) ) ;
96+ } ) ;
97+ readableStream . on ( 'end' , ( ) => {
98+ resolve ( Buffer . concat ( chunks . map ( chunk => Uint8Array . from ( chunk ) ) ) ) ;
99+ } ) ;
100+ readableStream . on ( 'error' , reject ) ;
101+ } ) ;
51102}
0 commit comments