Skip to content

Commit d52bd95

Browse files
author
Xziy
committed
fix file
1 parent 2440e74 commit d52bd95

File tree

5 files changed

+73
-71
lines changed

5 files changed

+73
-71
lines changed

adapters/mediafile/default/local.d.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ interface LoadMediaFilesProcess {
2727
config: MediaFileConfigInner;
2828
}
2929
export default class LocalMediaFileAdapter extends MediaFileAdapter {
30+
/** /////////////////////////////////
31+
* Process
32+
*/ process(url: string, type: MediaFileTypes, config: BaseConfig): Promise<{
33+
variant: ImageVariants;
34+
originalFilePath: string;
35+
}>;
3036
checkFileExist(mediaFile: MediaFileRecord): Promise<boolean>;
3137
private processing;
3238
private processingTimeout;
3339
loadMediaFilesProcessQueue: LoadMediaFilesProcess[];
3440
constructor(config: BaseConfig);
3541
getNameByUrl(url: string, ext: string, options?: any, salt?: string): string;
36-
process(url: string, type: MediaFileTypes, config: BaseConfig): Promise<{
37-
variant: ImageVariants;
38-
originalFilePath: string;
39-
}>;
4042
protected getPrefix(type?: MediaFileTypes, absolute?: boolean): string;
4143
getOriginalFilePath(url: string, type: MediaFileTypes): string;
4244
protected download(loadMediaFilesProcess: LoadMediaFilesProcess): Promise<string>;

adapters/mediafile/default/local.js

+64-65
Original file line numberDiff line numberDiff line change
@@ -33,63 +33,10 @@ const uuid_1 = require("uuid");
3333
const path = __importStar(require("path"));
3434
//@ts-ignore
3535
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-
// },
5636
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-zA-Z]+/g, "").substring(0, 7)}`;
88-
//baseName += `-${Math.floor(Date.now() / 1000)}`
89-
}
90-
baseName += `.${ext}`;
91-
return baseName;
92-
}
37+
/** /////////////////////////////////
38+
* Process
39+
*/ /////////////////////////////////
9340
async process(url, type, config) {
9441
const baseConfig = {
9542
format: "webp",
@@ -125,11 +72,68 @@ class LocalMediaFileAdapter extends MediaFileAdapter_1.default {
12572
result[key] = "/" + type + "/" + name[key];
12673
}
12774
}
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);
12896
return {
12997
variant: result,
13098
originalFilePath: this.getOriginalFilePath(url, type)
13199
};
132100
}
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-zA-Z]+/g, "").substring(0, 7)}`;
132+
//baseName += `-${Math.floor(Date.now() / 1000)}`
133+
}
134+
baseName += `.${ext}`;
135+
return baseName;
136+
}
133137
getPrefix(type, absolute = true) {
134138
const basePath = type ? path.join(".tmp/public", type) : path.join(".tmp/public");
135139
return absolute ? path.resolve(basePath) : basePath;
@@ -146,19 +150,14 @@ class LocalMediaFileAdapter extends MediaFileAdapter_1.default {
146150
}
147151
async download(loadMediaFilesProcess) {
148152
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));
150154
// Check if file exists
151155
if (!fs.existsSync(fullPathDl)) {
152156
let response;
153157
const url = loadMediaFilesProcess.url;
154158
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
162161
}
163162
else if (url.startsWith('http://') || url.startsWith('https://')) {
164163
// Handle HTTP/HTTPS URL
@@ -216,7 +215,7 @@ class LocalMediaFileAdapter extends MediaFileAdapter_1.default {
216215
sails.log.warn(`MediaFile size is not set for ${size}`);
217216
}
218217
await resizeMediaFile({
219-
srcPath: path.join(prefix, loadMediaFilesProcess.name.origin),
218+
srcPath: fullPathDl,
220219
dstPath: path.join(prefix, loadMediaFilesProcess.name[size]),
221220
size: mediafileItem
222221
});

adapters/mediafile/default/local.ts

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default class LocalMediaFileAdapter extends MediaFileAdapter {
9393
async function processFile(url: string, type: MediaFileTypes) {
9494
if (url.startsWith('file://')) {
9595
try {
96+
url = url.slice(7);
9697
const fullPathDl = path.join(this.getOriginalFilePath(url, type));
9798
const localFilePath = decodeURIComponent(new URL(url).pathname);
9899
sails.log.silly(`MF local > copy file: ${localFilePath} to ${fullPathDl}`);

libs/adminpanel/ProductMediaManager/Items.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ImageItem extends AbstractMediaManager_1.File {
1616
}
1717
async upload(file, filename, origFileName, group) {
1818
const mfAdater = await Adapter.getMediaFileAdapter();
19-
const mediaFile = await mfAdater.toProcess(file.fd, "dish", "image");
19+
const mediaFile = await mfAdater.toProcess(`file://${file.fd}`, "dish", "image");
2020
let parent = ConvertType_1.ConvertType.MF2Item(mediaFile);
2121
return [parent];
2222
}

libs/adminpanel/ProductMediaManager/Items.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class ImageItem extends File<MediaManagerItem> {
1717
public type: MediaFileType = "image";
1818
public async upload(file: UploaderFile, filename: string, origFileName: string, group?: string): Promise<MediaManagerItem[]> {
1919
const mfAdater = await Adapter.getMediaFileAdapter();
20-
const mediaFile = await mfAdater.toProcess(file.fd, "dish", "image");
20+
const mediaFile = await mfAdater.toProcess(`file://${file.fd}`, "dish", "image");
2121
let parent: MediaManagerItem = ConvertType.MF2Item(mediaFile);
2222
return [parent];
2323
}

0 commit comments

Comments
 (0)