@@ -39,15 +39,24 @@ public function getThumbnail(Request $request)
39
39
$ pdfUpload_Location = env ('PDF_UPLOAD ' );
40
40
$ pdfPool_Location = env ('PDF_POOL ' );
41
41
$ currentFileName = basename ($ files );
42
- $ pdfFileName = str_replace (' ' , '_ ' , $ currentFileName );
43
- $ pdfRealExtension = pathinfo ($ pdfFileName , PATHINFO_EXTENSION );
44
- $ newFilePath = Storage::disk ('local ' )->path ('public/ ' .$ pdfUpload_Location .'/ ' .$ pdfFileName );
45
- $ thumbnailFilePath = Storage::disk ('local ' )->path ('public/ ' .$ pdfThumbnail_Location .'/ ' .$ pdfFileName .'.png ' );
42
+ $ trimPhase1 = str_replace (' ' , '_ ' , $ currentFileName );
43
+ $ newFileNameWithoutExtension = str_replace ('. ' , '_ ' , $ trimPhase1 );
44
+ $ pdfRealExtension = pathinfo ($ currentFileName , PATHINFO_EXTENSION );
45
+ $ pdfRealName = pathinfo ($ trimPhase1 , PATHINFO_FILENAME );
46
+ $ newFormattedFilename = str_replace ('_ ' .$ pdfRealExtension , '' , $ newFileNameWithoutExtension );
47
+ $ minioUpload = Storage::disk ('minio ' )->get ($ pdfUpload_Location .'/ ' .$ trimPhase1 );
48
+ if (Storage::disk ('local ' )->exists ('public/ ' .$ pdfUpload_Location .'/ ' .$ newFormattedFilename .'. ' .$ pdfRealExtension )) {
49
+ Storage::disk ('local ' )->delete ('public/ ' .$ pdfUpload_Location .'/ ' .$ newFormattedFilename .'. ' .$ pdfRealExtension );
50
+ }
51
+ file_put_contents (Storage::disk ('local ' )->path ('public/ ' .$ pdfUpload_Location .'/ ' .$ newFormattedFilename .'. ' .$ pdfRealExtension ), $ minioUpload );
52
+ $ newFilePath = Storage::disk ('local ' )->path ('public/ ' .$ pdfUpload_Location .'/ ' .$ newFormattedFilename .'. ' .$ pdfRealExtension );
53
+ $ thumbnailFilePath = Storage::disk ('local ' )->path ('public/ ' .$ pdfThumbnail_Location .'/ ' .$ pdfRealName .'.png ' );
46
54
try {
55
+ ini_set ("pcre.backtrack_limit " , "5000000 " );
47
56
Settings::setPdfRendererPath (base_path ('vendor/mpdf/mpdf ' ));
48
57
Settings::setPdfRendererName ('MPDF ' );
49
58
50
- $ pdfPath = Storage::disk ('local ' )->path ('public/ ' .$ pdfPool_Location .'/ ' .$ pdfFileName );
59
+ $ pdfPath = Storage::disk ('local ' )->path ('public/ ' .$ pdfPool_Location .'/ ' .$ newFormattedFilename . ' . ' . $ pdfRealExtension );
51
60
if ($ pdfRealExtension == 'docx ' || $ pdfRealExtension == 'doc ' ) {
52
61
$ phpWord = WordIOFactory::load ($ newFilePath );
53
62
$ phpWord ->save ($ pdfPath , 'PDF ' );
@@ -60,7 +69,7 @@ public function getThumbnail(Request $request)
60
69
return $ this ->returnFileMesage (
61
70
400 ,
62
71
'Failed to generate thumbnail ! ' ,
63
- $ pdfFileName ,
72
+ $ pdfRealName ,
64
73
'Invalid or unsupported file extension: ' .$ pdfRealExtension
65
74
);
66
75
}
@@ -69,18 +78,45 @@ public function getThumbnail(Request $request)
69
78
->format (\Spatie \PdfToImage \Enums \OutputFormat::Png)
70
79
->quality (90 )
71
80
->save ($ thumbnailFilePath );
72
- return $ this ->returnFileMesage (
73
- 201 ,
74
- 'Thumbnail generated ! ' ,
75
- Storage::disk ('local ' )->url (env ('PDF_IMG_POOL ' ).'/ ' .$ pdfFileName .'.png ' ),
76
- $ pdfFileName
77
- );
81
+ if (Storage::disk ('local ' )->exists ('public/ ' .$ pdfThumbnail_Location .'/ ' .$ pdfRealName .'.png ' )) {
82
+ Storage::disk ('minio ' )->put ($ pdfThumbnail_Location .'/ ' .$ pdfRealName .'.png ' , file_get_contents ($ thumbnailFilePath ));
83
+ Storage::disk ('local ' )->delete ('public/ ' .$ pdfThumbnail_Location .'/ ' .$ pdfRealName .'.png ' );
84
+ Storage::disk ('local ' )->delete ('public/ ' .$ pdfPool_Location .'/ ' .$ newFormattedFilename .'. ' .$ pdfRealExtension );
85
+ try {
86
+ if (Storage::disk ('minio ' )->exists ($ pdfThumbnail_Location .'/ ' .$ pdfRealName .'.png ' )) {
87
+ return $ this ->returnFileMesage (
88
+ 201 ,
89
+ 'Thumbnail generated ! ' ,
90
+ Storage::disk ('minio ' )->temporaryUrl (
91
+ env ('PDF_IMG_POOL ' ).'/ ' .$ pdfRealName .'.png ' ,
92
+ now ()->addMinutes (5 )
93
+ ),
94
+ Storage::disk ('local ' )->url (env ('PDF_IMG_POOL ' ).'/ ' .$ pdfRealName .'.png ' ),
95
+ null ,
96
+ );
97
+ } else {
98
+ return $ this ->returnFileMesage (
99
+ 400 ,
100
+ 'Failed to upload file ! ' ,
101
+ $ pdfFileName ,
102
+ $ pdfFileName .' could not be found in the object storage '
103
+ );
104
+ }
105
+ } catch (\Exception $ e ) {
106
+ return $ this ->returnFileMesage (
107
+ 400 ,
108
+ 'Failed to upload thumbnail to object storage ! ' ,
109
+ $ pdfFileName ,
110
+ $ e ->getMessage ()
111
+ );
112
+ }
113
+ }
78
114
} catch (Exception $ e ) {
79
115
return $ this ->returnFileMesage (
80
116
500 ,
81
117
'Failed to generate thumbnail ! ' ,
82
- $ pdfFileName ,
83
- $ pdfFileName . ' could not generate thumbnail with error: ' .$ e ->getMessage ()
118
+ $ pdfRealName ,
119
+ ' Could not generate thumbnail with error: ' .$ e ->getMessage ()
84
120
);
85
121
}
86
122
}
0 commit comments