Skip to content

Commit

Permalink
feat(#14): FilesystemWeb plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pokeghosst committed Oct 17, 2024
1 parent 17056eb commit e3b5182
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pokebook/src/lib/driver/PoemCacheDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class PoemCacheDriver {
public static async isCachePresent(storage: string) {
return (
await Filesystem.exists({
path: `poems_${storage}.json`
path: `/poems_${storage}.json`
})
).exists;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ export default class PoemCacheDriver {
static async writeToCache(storage: string, data: PoemCacheRecord[]) {
console.log('writing to cache...');
await Filesystem.writeFile({
path: `poems_${storage}.json`,
path: `/poems_${storage}.json`,
data: JSON.stringify(data)
});
}
Expand Down
13 changes: 11 additions & 2 deletions pokebook/src/lib/driver/PoemLocalStorageDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

import { XMLBuilder, XMLParser } from 'fast-xml-parser';
import { Filesystem } from '../plugins/Filesystem';
import type { PoemFileEntity, PoemEntity } from '../types';

import type { IPoemStorageDriver } from './IPoemStorageDriver';

import type { PoemEntity, PoemFileEntity } from '../types';

export const PoemLocalStorageDriver: IPoemStorageDriver = {
listPoems: async function (): Promise<PoemFileEntity[]> {
const files = (await Filesystem.readDir({ path: '/' })).entries;
Expand All @@ -45,7 +47,7 @@ export const PoemLocalStorageDriver: IPoemStorageDriver = {
const now = Date.now();

const { uri } = await Filesystem.writeFile({
path: `${poem.name}_${now}.xml`,
path: `/${poem.name}_${now}.xml`,
data: new XMLBuilder({ format: true }).build(poem)
});

Expand All @@ -56,6 +58,13 @@ export const PoemLocalStorageDriver: IPoemStorageDriver = {
path: poemUri,
data: new XMLBuilder({ format: true }).build(poem)
});
const timestamp = poemUri.split('/')[1].split(/_|\.xml/)[1];
const newFileUri = `/${poem.name}_${timestamp}.xml`;
await Filesystem.rename({
from: poemUri,
to: newFileUri
});
return newFileUri;
},
deletePoem: async function (poemUri: string): Promise<void> {
await Filesystem.deleteFile({ path: poemUri });
Expand Down
28 changes: 19 additions & 9 deletions pokebook/src/lib/plugins/FilesystemWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class FilesystemWeb implements FilesystemPlugin {
const uri = await this.getDb().files.put(
{
content: options.data,
path: `/${options.path}`,
path: `${options.path}`,
ctime: now,
mtime: now
},
Expand All @@ -68,14 +68,11 @@ export class FilesystemWeb implements FilesystemPlugin {
return { uri };
}
async readFile(options: ReadFileOptions): Promise<ReadFileResult> {
const data = await this.getDb().files.where('path').equals(options.path).first();
const entry = await this.getDb().files.where('path').equals(options.path).first();

if (data?.content) {
return { data: data.content };
} else {
// TODO: Handle properly
throw new Error('errors.unknown');
}
if (!entry) throw new Error('File does not exist!');

return { data: entry.content };
}
async deleteFile(options: DeleteFileOptions): Promise<void> {
await this.getDb().files.delete(options.path);
Expand All @@ -96,5 +93,18 @@ export class FilesystemWeb implements FilesystemPlugin {
)
};
}
rename(options: RenameOptions): Promise<void> {}
async rename(options: RenameOptions): Promise<void> {
if (options.from === options.to) return;

const entry = await this.getDb().files.where('path').equals(options.from).first();

if (!entry) throw new Error('File does not exist!');

await this.getDb().files.delete(options.from);

await this.getDb().files.add({
...entry,
path: options.to
});
}
}

0 comments on commit e3b5182

Please sign in to comment.