diff --git a/modules/static_server.js b/modules/static_server.js index eb0a071..fe3d36e 100644 --- a/modules/static_server.js +++ b/modules/static_server.js @@ -59,10 +59,13 @@ export default class StaticHttpServer { } getUrl(filePath, withHost = false) { - let virtualPath = filePath - .replace(this.config.root, '') - .replace(path.sep, '/'); - virtualPath = virtualPath.startWith('/') ? virtualPath : `/${virtualPath}`; + let virtualPath = !filePath || filePath === null + ? '' + : filePath.replace(this.config.root, '').replace(path.sep, '/'); + this.log('get static url:', filePath, virtualPath); + if (virtualPath === '') return virtualPath; + + virtualPath = virtualPath.startsWith('/') ? virtualPath : `/${virtualPath}`; return `${ withHost ? `http://${this.config.host}:${this.config.port}` : '' }${virtualPath}`; @@ -126,9 +129,14 @@ Header: ${chalk.green(JSON.stringify(req.headers))} res.end(files.join(' ')); } } catch (err) { + this.log('error', 'handler', err); res.statusCode = 404; res.setHeader('content-type', 'text/plain'); - res.end(`${req.url} is not a file.`); + res.end( + process.env.NODE_ENV === 'development' + ? `request ${req.url} error, message: ${err.message}` + : `request ${req.url} fail.` + ); } } @@ -148,7 +156,7 @@ Header: ${chalk.green(JSON.stringify(req.headers))} } cache(req, res, fileStats) { - this.setCache(res); + this.setCache(res, fileStats); const lastModified = req.headers['if-modified-since']; const etag = req.headers['if-none-match']; diff --git a/src/layout/AppMain/Single.tsx b/src/layout/AppMain/Single.tsx index 1a6eec2..0455584 100644 --- a/src/layout/AppMain/Single.tsx +++ b/src/layout/AppMain/Single.tsx @@ -105,7 +105,7 @@ const Index = () => { setProcessing(false); if (current.status === TtsFileStatus.FAIL) { - message.error(current.error); + message.error(current.error?.message); } } ); diff --git a/src/utils/core.ts b/src/utils/core.ts index 11f8658..c3e1f92 100644 --- a/src/utils/core.ts +++ b/src/utils/core.ts @@ -41,6 +41,9 @@ export const delDirPath = (_path) => { } }; +/** + * 声明静态服务器 + */ const staticServer = new StaticHttpServer({ port: cacheStaticServerPort, root: fileCachePath @@ -57,6 +60,8 @@ export const appReset = () => { staticServer.serve(); }; +export const staticUrl = (filePath: string) => staticServer.getUrl(filePath, true); + export const checkDirExist = ( _path?: string, _tipIfNoExists?: string @@ -372,7 +377,7 @@ export const ttsTasksRun = async ( speed: appSetting.ttsSetting.speedRate, pitch: appSetting.ttsSetting.pitchRate }); - // finfo.audioUrl = staticServer.get(req.ur); + finfo.audioUrl = staticUrl(finfo.taskId ?? ''); setSuccess(finfo); } } catch (error) {