-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathFileStore.ts
More file actions
46 lines (41 loc) · 1.14 KB
/
FileStore.ts
File metadata and controls
46 lines (41 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import fs from 'fs';
import path from 'path'
import StoreLogger from './StoreLogger'
import IStore from './IStore'
/**
* A class that allows for messages to be stored in
* a local file system
*
* Note this class implements the IStore interface
*/
export default class FileStore implements IStore {
directory: string
logger: StoreLogger
constructor(_directory: string, _logger: StoreLogger) {
this.directory = _directory;
this.logger = _logger;
}
public save(id: number, message: string): void {
this.logger.saving(id);
var fileFullName = this.getFileInfo(id);
try {
fs.writeFileSync(fileFullName, message)
} catch (err) {
this.logger.errorSaving(id);
}
this.logger.saved(id)
}
public read(id: number): string {
this.logger.readingFilestore(id)
var fileFullName = this.getFileInfo(id);
var exists = fs.existsSync(fileFullName);
if(!exists) {
this.logger.didNotFind(id);
return undefined
}
return fs.readFileSync(fileFullName, {encoding: 'ASCII'});
}
public getFileInfo(id: number): string {
return path.join(__dirname, this.directory, `${id}.txt`)
}
}