Skip to content

Commit

Permalink
feat: 集成讯飞
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Nov 19, 2021
1 parent b320252 commit fecf679
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
20 changes: 14 additions & 6 deletions modules/static_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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.`
);
}
}

Expand All @@ -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'];
Expand Down
2 changes: 1 addition & 1 deletion src/layout/AppMain/Single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const Index = () => {

setProcessing(false);
if (current.status === TtsFileStatus.FAIL) {
message.error(current.error);
message.error(current.error?.message);
}
}
);
Expand Down
7 changes: 6 additions & 1 deletion src/utils/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export const delDirPath = (_path) => {
}
};

/**
* 声明静态服务器
*/
const staticServer = new StaticHttpServer({
port: cacheStaticServerPort,
root: fileCachePath
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit fecf679

Please sign in to comment.