@@ -33,63 +33,10 @@ const uuid_1 = require("uuid");
33
33
const path = __importStar ( require ( "path" ) ) ;
34
34
//@ts -ignore
35
35
const sharp_1 = __importDefault ( require ( "sharp" ) ) ;
36
- // images: {
37
- // adapter: 'imagemagick-local',
38
- // dish: {
39
- // format: process.env.IMAGES_DISH_FILE_FORMAT === undefined ? 'png' : process.env.IMAGES_DISH_FILE_FORMAT,
40
- // path: '/images',
41
- // resize: {
42
- // small: {
43
- // width: process.env.IMAGES_SMALL_SIZE_PX === undefined ? 600 : parseInt(process.env.IMAGES_SMALL_SIZE_PX),
44
- // height: process.env.IMAGES_SMALL_SIZE_PX === undefined ? 600 : parseInt(process.env.IMAGES_SMALL_SIZE_PX)
45
- // },
46
- // large: {
47
- // width: process.env.IMAGES_LARGE_SIZE_PX === undefined ? 900 : parseInt(process.env.IMAGES_LARGE_SIZE_PX),
48
- // }
49
- // }
50
- // },
51
- // group: {
52
- // format: process.env.IMAGES_GROUP_FILE_FORMAT === undefined ? 'png' : process.env.IMAGES_GROUP_FILE_FORMAT,
53
- // path: '/imagesG',
54
- // }
55
- // },
56
36
class LocalMediaFileAdapter extends MediaFileAdapter_1 . default {
57
- async checkFileExist ( mediaFile ) {
58
- let allFileExist = true ;
59
- if ( mediaFile && /* mediaFile.type === "image" && **/ typeof mediaFile . images === "object" && mediaFile . images !== null && Object . keys ( mediaFile . images ) . length ) {
60
- const images = mediaFile . images ;
61
- for ( const key in images ) {
62
- const imageFilePath = path . join ( this . getPrefix ( ) , images [ key ] ) ;
63
- try {
64
- await fs . promises . access ( imageFilePath , fs . constants . F_OK ) ;
65
- }
66
- catch ( error ) {
67
- // If the file does not exist, set the allFileExist flag to false
68
- sails . log . debug ( `LocalMediaFileAdapter > file not exist: ${ imageFilePath } ` ) ;
69
- allFileExist = false ;
70
- }
71
- }
72
- }
73
- return allFileExist ;
74
- }
75
- constructor ( config ) {
76
- super ( config ) ;
77
- this . processing = false ;
78
- this . loadMediaFilesProcessQueue = [ ] ;
79
- this . loadMediaFiles ( ) ;
80
- }
81
- getNameByUrl ( url , ext , options , salt = null ) {
82
- let baseName = url ;
83
- if ( options )
84
- baseName += JSON . stringify ( options ) ;
85
- baseName = ( 0 , uuid_1 . v5 ) ( baseName , this . UUID_NAMESPACE ) ;
86
- if ( salt ) {
87
- baseName += `-${ salt . toString ( ) . toLowerCase ( ) . replace ( / [ ^ a - z A - Z ] + / g, "" ) . substring ( 0 , 7 ) } ` ;
88
- //baseName += `-${Math.floor(Date.now() / 1000)}`
89
- }
90
- baseName += `.${ ext } ` ;
91
- return baseName ;
92
- }
37
+ /** /////////////////////////////////
38
+ * Process
39
+ */ /////////////////////////////////
93
40
async process ( url , type , config ) {
94
41
const baseConfig = {
95
42
format : "webp" ,
@@ -125,11 +72,68 @@ class LocalMediaFileAdapter extends MediaFileAdapter_1.default {
125
72
result [ key ] = "/" + type + "/" + name [ key ] ;
126
73
}
127
74
}
75
+ async function processFile ( url , type ) {
76
+ if ( url . startsWith ( 'file://' ) ) {
77
+ try {
78
+ url = url . slice ( 7 ) ;
79
+ const fullPathDl = path . join ( this . getOriginalFilePath ( url , type ) ) ;
80
+ const localFilePath = decodeURIComponent ( new URL ( url ) . pathname ) ;
81
+ sails . log . silly ( `MF local > copy file: ${ localFilePath } to ${ fullPathDl } ` ) ;
82
+ const prefix = this . getPrefix ( type , false ) ;
83
+ // Await each async operation to ensure completion before moving to the next step
84
+ await fs . promises . mkdir ( prefix , { recursive : true } ) ;
85
+ await fs . promises . copyFile ( localFilePath , fullPathDl ) ;
86
+ await fs . promises . unlink ( localFilePath ) ;
87
+ sails . log . silly ( `File copied and original deleted successfully.` ) ;
88
+ }
89
+ catch ( error ) {
90
+ sails . log . error ( `Failed to process file: ${ error . message } ` ) ;
91
+ }
92
+ }
93
+ }
94
+ // Somewhere in your main function or code where you need to call processFile:
95
+ await processFile ( url , type ) ;
128
96
return {
129
97
variant : result ,
130
98
originalFilePath : this . getOriginalFilePath ( url , type )
131
99
} ;
132
100
}
101
+ async checkFileExist ( mediaFile ) {
102
+ let allFileExist = true ;
103
+ if ( mediaFile && /* mediaFile.type === "image" && **/ typeof mediaFile . images === "object" && mediaFile . images !== null && Object . keys ( mediaFile . images ) . length ) {
104
+ const images = mediaFile . images ;
105
+ for ( const key in images ) {
106
+ const imageFilePath = path . join ( this . getPrefix ( ) , images [ key ] ) ;
107
+ try {
108
+ await fs . promises . access ( imageFilePath , fs . constants . F_OK ) ;
109
+ }
110
+ catch ( error ) {
111
+ // If the file does not exist, set the allFileExist flag to false
112
+ sails . log . debug ( `LocalMediaFileAdapter > file not exist: ${ imageFilePath } ` ) ;
113
+ allFileExist = false ;
114
+ }
115
+ }
116
+ }
117
+ return allFileExist ;
118
+ }
119
+ constructor ( config ) {
120
+ super ( config ) ;
121
+ this . processing = false ;
122
+ this . loadMediaFilesProcessQueue = [ ] ;
123
+ this . loadMediaFiles ( ) ;
124
+ }
125
+ getNameByUrl ( url , ext , options , salt = null ) {
126
+ let baseName = url ;
127
+ if ( options )
128
+ baseName += JSON . stringify ( options ) ;
129
+ baseName = ( 0 , uuid_1 . v5 ) ( baseName , this . UUID_NAMESPACE ) ;
130
+ if ( salt ) {
131
+ baseName += `-${ salt . toString ( ) . toLowerCase ( ) . replace ( / [ ^ a - z A - Z ] + / g, "" ) . substring ( 0 , 7 ) } ` ;
132
+ //baseName += `-${Math.floor(Date.now() / 1000)}`
133
+ }
134
+ baseName += `.${ ext } ` ;
135
+ return baseName ;
136
+ }
133
137
getPrefix ( type , absolute = true ) {
134
138
const basePath = type ? path . join ( ".tmp/public" , type ) : path . join ( ".tmp/public" ) ;
135
139
return absolute ? path . resolve ( basePath ) : basePath ;
@@ -146,19 +150,14 @@ class LocalMediaFileAdapter extends MediaFileAdapter_1.default {
146
150
}
147
151
async download ( loadMediaFilesProcess ) {
148
152
const prefix = this . getPrefix ( loadMediaFilesProcess . type ) ;
149
- const fullPathDl = path . join ( process . cwd ( ) , this . getOriginalFilePath ( loadMediaFilesProcess . url , loadMediaFilesProcess . type ) ) ;
153
+ const fullPathDl = path . join ( this . getOriginalFilePath ( loadMediaFilesProcess . url , loadMediaFilesProcess . type ) ) ;
150
154
// Check if file exists
151
155
if ( ! fs . existsSync ( fullPathDl ) ) {
152
156
let response ;
153
157
const url = loadMediaFilesProcess . url ;
154
158
if ( url . startsWith ( 'file://' ) ) {
155
- // Handle local file URL
156
- const localFilePath = decodeURIComponent ( new URL ( url ) . pathname ) ;
157
- sails . log . silly ( `MF local > copy file: ${ localFilePath } to ${ fullPathDl } ` ) ;
158
- fs . mkdirSync ( prefix , { recursive : true } ) ;
159
- fs . copyFileSync ( localFilePath , fullPathDl ) ;
160
- fs . unlinkSync ( localFilePath ) ;
161
- return ; // Exit the method since the file is copied
159
+ // // Handle local file URL
160
+ // It was moved in in process
162
161
}
163
162
else if ( url . startsWith ( 'http://' ) || url . startsWith ( 'https://' ) ) {
164
163
// Handle HTTP/HTTPS URL
@@ -216,7 +215,7 @@ class LocalMediaFileAdapter extends MediaFileAdapter_1.default {
216
215
sails . log . warn ( `MediaFile size is not set for ${ size } ` ) ;
217
216
}
218
217
await resizeMediaFile ( {
219
- srcPath : path . join ( prefix , loadMediaFilesProcess . name . origin ) ,
218
+ srcPath : fullPathDl ,
220
219
dstPath : path . join ( prefix , loadMediaFilesProcess . name [ size ] ) ,
221
220
size : mediafileItem
222
221
} ) ;
0 commit comments