Skip to content

Commit

Permalink
random filename generator issue fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrojs committed Nov 16, 2021
1 parent df91056 commit 6d38fc5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
52 changes: 32 additions & 20 deletions adapter/local.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs-extra')
const CoreAdapter = require('./CoreAdapter')
const path = require('path')
const { v4 } = require("uuid");
const kHandler = Symbol('handler')

class Local extends CoreAdapter {
Expand Down Expand Up @@ -74,26 +75,37 @@ class Local extends CoreAdapter {

let callback = Array.from(rest).find(arg => typeof arg == 'function')
let location = this.applyPathPrefix($path);
this.ensureDirectory(this.dirname(location), () => {
fs.writeFile(location, $contents, (error, data) => {
if (typeof this[kHandler].write == 'function') {
this[kHandler].write({
basePath: location,
path: $path,
options,
callback,
error,
content: $contents
})
} else {
callback(error, {
'type': 'file',
'path': location,
'message': 'File created successfully'
}, location)
}
})
});
fs.stat(location).then(res=>{
if(res.isDirectory()){
return path.join(location,v4())
}
return location
})
.catch(e=>location)
.then(location=>{
this.ensureDirectory(this.dirname(location), () => {
fs.writeFile(location, $contents, (error, data) => {
if (typeof this[kHandler].write == 'function') {
this[kHandler].write({
basePath: location,
path: $path,
options,
callback,
error,
content: $contents
})
} else {
callback(error, {
'type': 'file',
'path': location,
'message': 'File created successfully'
}, location)
}
})
});
})



}

Expand Down
9 changes: 2 additions & 7 deletions filesystemAdapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const path = require('path')
const { v4 } = require("uuid");
const FileNotFoundException = require('./FileNotFoundException')
const FileDeleteException = require('./fileDeleteException')
const FileUploadException = require('./fileUploadException')
Expand Down Expand Up @@ -69,11 +68,7 @@ class FilesystemAdapter {
}

put($path, content, options = {}) {
if (!options.filename) {
options.filename = v4()
}

$path = path.join($path, options.filename)

return new Promise((resolve, reject) => {
this[kAdapter].write($path, content, options, (err, data, basePath) => {
if (err) {
Expand All @@ -91,7 +86,7 @@ class FilesystemAdapter {
}

putFileAs($path, $file, $name, $options = {}) {
$options.filename = $name
$path = path.join($path,$name)
return this.put($path, $file.getBufferData(), $options);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ostro/filesystem",
"version": "0.0.0-alpha.0",
"version": "0.0.0-alpha.1",
"description": "filesystem module for OstroJS",
"main": "filesystemManager.js",
"scripts": {
Expand Down

0 comments on commit 6d38fc5

Please sign in to comment.