fix: 优化代码
This commit is contained in:
parent
f0468539dc
commit
c92f817d66
@ -96,7 +96,16 @@ export class UserApp {
|
|||||||
domain,
|
domain,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}).then((res) => res.json());
|
})
|
||||||
|
.then((res) => {
|
||||||
|
return res.json();
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
return {
|
||||||
|
code: 500,
|
||||||
|
message: err,
|
||||||
|
};
|
||||||
|
});
|
||||||
if (fetchRes?.code !== 200) {
|
if (fetchRes?.code !== 200) {
|
||||||
console.log('fetchRes is error', fetchRes);
|
console.log('fetchRes is error', fetchRes);
|
||||||
return null;
|
return null;
|
||||||
@ -121,6 +130,12 @@ export class UserApp {
|
|||||||
status[key] = false;
|
status[key] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async getLoaded() {
|
||||||
|
const app = this.app;
|
||||||
|
const user = this.user;
|
||||||
|
const key = 'user:app:' + app + ':' + user;
|
||||||
|
return status[key];
|
||||||
|
}
|
||||||
async setCacheData() {
|
async setCacheData() {
|
||||||
const app = this.app;
|
const app = this.app;
|
||||||
const user = this.user;
|
const user = this.user;
|
||||||
@ -214,6 +229,9 @@ export class UserApp {
|
|||||||
// 删除所有文件
|
// 删除所有文件
|
||||||
deleteUserAppFiles(user, app);
|
deleteUserAppFiles(user, app);
|
||||||
}
|
}
|
||||||
|
fileCheck(file: string) {
|
||||||
|
return checkFileExistsSync(file);
|
||||||
|
}
|
||||||
async close() {
|
async close() {
|
||||||
// 关闭连接
|
// 关闭连接
|
||||||
await redis.quit();
|
await redis.quit();
|
||||||
@ -229,25 +247,35 @@ export const downloadUserAppFiles = async (user: string, app: string, data: type
|
|||||||
fs.mkdirSync(uploadFiles, { recursive: true });
|
fs.mkdirSync(uploadFiles, { recursive: true });
|
||||||
}
|
}
|
||||||
const newFiles = [];
|
const newFiles = [];
|
||||||
if (data.type === 'local') {
|
try {
|
||||||
// local copy file
|
if (data.type === 'local') {
|
||||||
for (let i = 0; i < files.length; i++) {
|
// local copy file
|
||||||
const file = files[i];
|
for (let i = 0; i < files.length; i++) {
|
||||||
const copyFile = path.join(fileStore, file.path);
|
const file = files[i];
|
||||||
const destFile = path.join(uploadFiles, file.name);
|
const copyFile = path.join(fileStore, file.path);
|
||||||
const destDir = path.dirname(destFile); // 获取目标文件所在的目录路径
|
const destFile = path.join(uploadFiles, file.name);
|
||||||
// 检查目录是否存在,如果不存在则创建
|
const destDir = path.dirname(destFile); // 获取目标文件所在的目录路径
|
||||||
if (!checkFileExistsSync(destDir)) {
|
// 检查目录是否存在,如果不存在则创建
|
||||||
fs.mkdirSync(destDir, { recursive: true }); // 递归创建目录
|
if (!checkFileExistsSync(destDir)) {
|
||||||
|
fs.mkdirSync(destDir, { recursive: true }); // 递归创建目录
|
||||||
|
}
|
||||||
|
fs.copyFileSync(copyFile, destFile);
|
||||||
|
// const etag = await setEtag(fs.readFileSync(destFile, 'utf-8'));
|
||||||
|
const etag = nanoid();
|
||||||
|
newFiles.push({
|
||||||
|
name: file.name,
|
||||||
|
path: destFile.replace(fileStore, '') + '||' + etag,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
fs.copyFileSync(copyFile, destFile);
|
|
||||||
// const etag = await setEtag(fs.readFileSync(destFile, 'utf-8'));
|
|
||||||
const etag = nanoid();
|
|
||||||
newFiles.push({
|
|
||||||
name: file.name,
|
|
||||||
path: destFile.replace(fileStore, '') + '||' + etag,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
const userApp = new UserApp({ user, app });
|
||||||
|
userApp.clearCacheData();
|
||||||
|
return {
|
||||||
|
data: {
|
||||||
|
files: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
if (data.type === 'oss') {
|
if (data.type === 'oss') {
|
||||||
const serverPath = 'https://' + resources + '/';
|
const serverPath = 'https://' + resources + '/';
|
||||||
@ -292,7 +320,11 @@ export const deleteUserAppFiles = async (user: string, app: string) => {
|
|||||||
try {
|
try {
|
||||||
fs.rmSync(uploadFiles, { recursive: true });
|
fs.rmSync(uploadFiles, { recursive: true });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('deleteUserAppFiles', err);
|
if (err.code === 'ENOENT') {
|
||||||
|
// 文件不存在
|
||||||
|
} else {
|
||||||
|
console.error('deleteUserAppFiles', err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// console.log('deleteUserAppFiles', res);
|
// console.log('deleteUserAppFiles', res);
|
||||||
};
|
};
|
||||||
|
@ -17,7 +17,6 @@ const { api, domain, allowedOrigins } = useConfig<{
|
|||||||
}>();
|
}>();
|
||||||
|
|
||||||
const fileStore = useFileStore('upload');
|
const fileStore = useFileStore('upload');
|
||||||
console.log('filePath', fileStore);
|
|
||||||
const noProxyUrl = ['/', '/favicon.ico'];
|
const noProxyUrl = ['/', '/favicon.ico'];
|
||||||
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||||
const dns = getDNS(req);
|
const dns = getDNS(req);
|
||||||
@ -42,7 +41,6 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
|||||||
// app = 'codeflow';
|
// app = 'codeflow';
|
||||||
// domainApp = true;
|
// domainApp = true;
|
||||||
} else {
|
} else {
|
||||||
// 生产环境
|
|
||||||
// 验证域名
|
// 验证域名
|
||||||
if (dns.hostName !== domain) {
|
if (dns.hostName !== domain) {
|
||||||
// redis获取域名对应的用户和应用
|
// redis获取域名对应的用户和应用
|
||||||
@ -172,6 +170,14 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
|||||||
const [indexFilePath, etag] = indexFile.split('||');
|
const [indexFilePath, etag] = indexFile.split('||');
|
||||||
const contentType = getContentType(indexFilePath);
|
const contentType = getContentType(indexFilePath);
|
||||||
const isHTML = contentType.includes('html');
|
const isHTML = contentType.includes('html');
|
||||||
|
const filePath = path.join(fileStore, indexFilePath);
|
||||||
|
if (!userApp.fileCheck(filePath)) {
|
||||||
|
res.writeHead(500, { 'Content-Type': 'text/html' });
|
||||||
|
res.write('File expired, Not Found\n');
|
||||||
|
res.end();
|
||||||
|
await userApp.clearCacheData();
|
||||||
|
return;
|
||||||
|
}
|
||||||
// 如果 content是 'application/octet-stream' 会下载文件, 添加文件后缀
|
// 如果 content是 'application/octet-stream' 会下载文件, 添加文件后缀
|
||||||
if (contentType === 'application/octet-stream') {
|
if (contentType === 'application/octet-stream') {
|
||||||
// 提取文件名,只保留文件名而不是整个路径
|
// 提取文件名,只保留文件名而不是整个路径
|
||||||
@ -181,9 +187,9 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
|||||||
// 不存在的文件,返回indexFile的文件
|
// 不存在的文件,返回indexFile的文件
|
||||||
res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': isHTML ? 'no-cache' : 'public, max-age=3600' });
|
res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': isHTML ? 'no-cache' : 'public, max-age=3600' });
|
||||||
|
|
||||||
const filePath = path.join(fileStore, indexFilePath);
|
|
||||||
const readStream = fs.createReadStream(filePath);
|
const readStream = fs.createReadStream(filePath);
|
||||||
readStream.pipe(res);
|
readStream.pipe(res);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
const [appFilePath, eTag] = appFile.split('||');
|
const [appFilePath, eTag] = appFile.split('||');
|
||||||
@ -207,8 +213,17 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
|||||||
'Cache-Control': isHTML ? 'no-cache' : 'public, max-age=3600', // 设置缓存时间为 1 小时
|
'Cache-Control': isHTML ? 'no-cache' : 'public, max-age=3600', // 设置缓存时间为 1 小时
|
||||||
ETag: eTag,
|
ETag: eTag,
|
||||||
});
|
});
|
||||||
|
if (!userApp.fileCheck(filePath)) {
|
||||||
|
console.error('File expired', filePath);
|
||||||
|
res.writeHead(500, { 'Content-Type': 'text/html' });
|
||||||
|
res.write('File expired\n');
|
||||||
|
res.end();
|
||||||
|
await userApp.clearCacheData();
|
||||||
|
return;
|
||||||
|
}
|
||||||
const readStream = fs.createReadStream(filePath);
|
const readStream = fs.createReadStream(filePath);
|
||||||
readStream.pipe(res);
|
readStream.pipe(res);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -7,5 +7,5 @@ export const getDNS = (req: http.IncomingMessage) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const isLocalhost = (hostName: string) => {
|
export const isLocalhost = (hostName: string) => {
|
||||||
return hostName.includes('localhost');
|
return hostName.includes('localhost') || hostName.includes('192.168');
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user