Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(core/web): avoid appendFile/writeFile to overwrite existing directory entry #1782

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/src/web/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
async writeFile(options: FileWriteOptions): Promise<FileWriteResult> {
const path: string = this.getPath(options.directory, options.path);
const data = options.data;

let occupiedEntry = await this.dbRequest('get', [path]) as EntryObj;
if (occupiedEntry && occupiedEntry.type === 'directory')
throw('The supplied path is a directory.');

// const encoding = options.encoding;
const parentPath = path.substr(0, path.lastIndexOf('/'));

Expand Down Expand Up @@ -182,12 +187,17 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {

const now = Date.now();
let ctime = now;

let occupiedEntry = await this.dbRequest('get', [path]) as EntryObj;
if (occupiedEntry && occupiedEntry.type === 'directory')
throw('The supplied path is a directory.');

let parentEntry = await this.dbRequest('get', [parentPath]) as EntryObj;
if (parentEntry === undefined) {
const parentArgPath = parentPath.substr(parentPath.indexOf('/', 1));
await this.mkdir({path: parentArgPath, directory: options.directory, createIntermediateDirectories: true})
}
let occupiedEntry = await this.dbRequest('get', [path]) as EntryObj;

if (occupiedEntry !== undefined) {
data = occupiedEntry.content + data;
ctime = occupiedEntry.ctime;
Expand Down