Skip to content

Commit

Permalink
fix(core/web): avoid appendFile/writeFile to overwrite existing direc…
Browse files Browse the repository at this point in the history
…tory entry (#1782)
  • Loading branch information
Chris Lloyd authored and jcesarmobile committed Jul 22, 2019
1 parent bbab8ee commit ad1a4e4
Showing 1 changed file with 11 additions and 1 deletion.
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

0 comments on commit ad1a4e4

Please # to comment.