-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathFileStore.ts
More file actions
35 lines (30 loc) · 935 Bytes
/
FileStore.ts
File metadata and controls
35 lines (30 loc) · 935 Bytes
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
import fs from 'fs';
import path from 'path';
import IFileLocator from './IFileLocator';
import IStoreWriter from './IStoreWriter';
import IStoreReader from './IStoreReader';
/**
* A class that allows for messages to be stored in
* a local file system
*/
export default class FileStore implements IStoreReader, IStoreWriter, IFileLocator {
directory: string
constructor(_directory: string) {
this.directory = _directory;
}
public save(id: number, message: string): void {
var fileFullName = this.getFileInfo(id);
fs.writeFileSync(fileFullName, message)
}
public read(id: number): string {
var fileFullName = this.getFileInfo(id);
var exists = fs.existsSync(fileFullName);
if(!exists) {
return undefined
}
return fs.readFileSync(fileFullName, {encoding: 'ASCII'});
}
public getFileInfo(id: number): string {
return path.join(__dirname, this.directory, `${id}.txt`)
}
}