Merge branch 'fix/path#391'

This commit is contained in:
xiaofeng.mxf 2019-03-24 21:26:44 +08:00
commit fc4befa209
2 changed files with 62 additions and 41 deletions

View File

@ -126,12 +126,12 @@ class Recorder extends events.EventEmitter {
* *
*/ */
updateRecordWsMessage(id, message) { updateRecordWsMessage(id, message) {
const cachePath = this.cachePath;
if (id < 0) return; if (id < 0) return;
try { try {
const recordWsMessageFile = path.join(cachePath, WS_MESSAGE_FILE_PRFIX + id); this.getCacheFile(WS_MESSAGE_FILE_PRFIX + id, (err, recordWsMessageFile) => {
if (err) return;
fs.appendFile(recordWsMessageFile, wsMessageStingify(message) + ',', () => {}); fs.appendFile(recordWsMessageFile, wsMessageStingify(message) + ',', () => {});
});
} catch (e) { } catch (e) {
console.error(e); console.error(e);
logUtil.error(e.message + e.stack); logUtil.error(e.message + e.stack);
@ -172,15 +172,16 @@ class Recorder extends events.EventEmitter {
updateRecordBody(id, info) { updateRecordBody(id, info) {
const self = this; const self = this;
const cachePath = self.cachePath;
if (id === -1) return; if (id === -1) return;
if (!id || typeof info.resBody === 'undefined') return; if (!id || typeof info.resBody === 'undefined') return;
//add to body map //add to body map
//ignore image data //ignore image data
const bodyFile = path.join(cachePath, BODY_FILE_PRFIX + id); self.getCacheFile(BODY_FILE_PRFIX + id, (err, bodyFile) => {
fs.writeFile(bodyFile, info.resBody, () => {}); if (err) return;
fs.writeFile(bodyFile, info.resBody, () => {});
});
} }
/** /**
@ -189,19 +190,23 @@ class Recorder extends events.EventEmitter {
*/ */
getBody(id, cb) { getBody(id, cb) {
const self = this; const self = this;
const cachePath = self.cachePath;
if (id < 0) { if (id < 0) {
cb && cb(''); cb && cb('');
return;
} }
self.getCacheFile(BODY_FILE_PRFIX + id, (error, bodyFile) => {
const bodyFile = path.join(cachePath, BODY_FILE_PRFIX + id); if (error) {
fs.access(bodyFile, fs.F_OK || fs.R_OK, (err) => { cb && cb(error);
if (err) { return;
cb && cb(err);
} else {
fs.readFile(bodyFile, cb);
} }
fs.access(bodyFile, fs.F_OK || fs.R_OK, (err) => {
if (err) {
cb && cb(err);
} else {
fs.readFile(bodyFile, cb);
}
});
}); });
} }
@ -270,36 +275,39 @@ class Recorder extends events.EventEmitter {
* *
*/ */
getDecodedWsMessage(id, cb) { getDecodedWsMessage(id, cb) {
const self = this;
const cachePath = self.cachePath;
if (id < 0) { if (id < 0) {
cb && cb([]); cb && cb([]);
return;
} }
const wsMessageFile = path.join(cachePath, WS_MESSAGE_FILE_PRFIX + id); this.getCacheFile(WS_MESSAGE_FILE_PRFIX + id, (outError, wsMessageFile) => {
fs.access(wsMessageFile, fs.F_OK || fs.R_OK, (err) => { if (outError) {
if (err) { cb && cb(outError);
cb && cb(err); return;
} else {
fs.readFile(wsMessageFile, 'utf8', (error, content) => {
if (error) {
cb && cb(err);
}
try {
// remove the last dash "," if it has, since it's redundant
// and also add brackets to make it a complete JSON structure
content = `[${content.replace(/,$/, '')}]`;
const messages = JSON.parse(content);
cb(null, messages);
} catch (e) {
console.error(e);
logUtil.error(e.message + e.stack);
cb(e);
}
});
} }
fs.access(wsMessageFile, fs.F_OK || fs.R_OK, (err) => {
if (err) {
cb && cb(err);
} else {
fs.readFile(wsMessageFile, 'utf8', (error, content) => {
if (error) {
cb && cb(err);
}
try {
// remove the last dash "," if it has, since it's redundant
// and also add brackets to make it a complete JSON structure
content = `[${content.replace(/,$/, '')}]`;
const messages = JSON.parse(content);
cb(null, messages);
} catch (e) {
console.error(e);
logUtil.error(e.message + e.stack);
cb(e);
}
});
}
});
}); });
} }
@ -330,6 +338,19 @@ class Recorder extends events.EventEmitter {
const self = this; const self = this;
proxyUtil.deleteFolderContentsRecursive(self.cachePath, true); proxyUtil.deleteFolderContentsRecursive(self.cachePath, true);
} }
getCacheFile(fileName, cb) {
const self = this;
const cachePath = self.cachePath;
const filepath = path.join(cachePath, fileName);
if (filepath.indexOf(cachePath) !== 0) {
cb && cb(new Error('invalid cache file path'));
} else {
cb && cb(null, filepath);
return filepath;
}
}
} }
module.exports = Recorder; module.exports = Recorder;

View File

@ -161,7 +161,7 @@ class webInterface extends events.EventEmitter {
} }
}); });
} else { } else {
res.end({}); res.end('');
} }
}); });