randalthor-dropboxapi
Advanced tools
Comparing version 1.0.3 to 1.1.0
@@ -5,3 +5,7 @@ const Dropbox = require("dropbox").Dropbox; | ||
const path = require("path"); | ||
const { promisify } = require("util"); | ||
const writeFile = promisify(fs.writeFile); | ||
const readFile = promisify(fs.readFile); | ||
class Client { | ||
@@ -18,3 +22,3 @@ constructor(ACCESS_TOKEN) { | ||
uploadLocalSync(location, options) { | ||
async uploadLocalSync(location, options) { | ||
if (!options) { | ||
@@ -26,17 +30,14 @@ options = { | ||
} | ||
options.contents = fs.readFileSync(location); | ||
const imageStats = fs.statSync(location); | ||
if (imageStats.size < this.UPLOAD_FILE_SIZE_LIMIT) { | ||
this.dpx | ||
.filesUploadSync(options) | ||
.then(res => { | ||
console.log(res); | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
}); | ||
try { | ||
const image = await readFile(location); | ||
options.contents = image; | ||
if (image.length < this.UPLOAD_FILE_SIZE_LIMIT) { | ||
await this.dpx.filesUpload(options); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
async uploadLocal(location, options) { | ||
async uploadBinarySync(data, options) { | ||
if (!options) { | ||
@@ -48,34 +49,18 @@ options = { | ||
} | ||
fs.readFile(location, (err, data) => { | ||
if (err) console.log(err); | ||
else { | ||
options.contents = data; | ||
fs.stat(location, (error, stats) => { | ||
if (error) console.error(error); | ||
else { | ||
if (stats.size < this.UPLOAD_FILE_SIZE_LIMIT) { | ||
this.dpx | ||
.filesUpload(options) | ||
.then(res => { | ||
console.log(res); | ||
}) | ||
.catch(er => { | ||
console.log(er); | ||
}); | ||
} | ||
} | ||
}); | ||
options.contents = data; | ||
if (data.length < this.UPLOAD_FILE_SIZE_LIMIT) { | ||
try { | ||
await this.dpx.filesUpload(options); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}); | ||
} | ||
} | ||
async downloadLocal(image, location, options) { | ||
async downloadLocalSync(image, location, options) { | ||
if (!options) options = {}; | ||
if (!location) location = ""; | ||
try { | ||
let result = await this.dpx.filesDownload({ path: "/" + image }, options); | ||
if (!result) console.log("Error Downloading image Locally"); | ||
fs.writeFile(location + image, result.fileBinary, err => { | ||
if (err) console.log(err); | ||
}); | ||
const binary = await this.downloadBinarySync(image, options); | ||
await writeFile(location + image, binary); | ||
} catch (error) { | ||
@@ -86,30 +71,16 @@ console.error(error); | ||
downloadLocalSync(image, location, options) { | ||
async downloadBinarySync(image, options) { | ||
if (!options) options = {}; | ||
if (!location) location = ""; | ||
this.dpx | ||
.filesDownload({ path: "/" + image }, options) | ||
.then(res => { | ||
fs.writeFileSync(location + image, res.fileBinary); | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
}); | ||
try { | ||
const result = await this.dpx.filesDownload( | ||
{ path: "/" + image }, | ||
options | ||
); | ||
return result.fileBinary; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
downloadBinarySync(image, location, options) { | ||
if (!options) options = {}; | ||
if (!location) location = ""; | ||
this.dpx | ||
.filesDownload({ path: "/" + image }, options) | ||
.then(res => { | ||
return res.fileBinary; | ||
}) | ||
.catch(err => { | ||
console.log(err); | ||
return undefined; | ||
}); | ||
} | ||
} | ||
module.exports.Client = Client; |
{ | ||
"name": "randalthor-dropboxapi", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Client Wrapper for Dropbox Node API", | ||
@@ -17,2 +17,2 @@ "main": "client.js", | ||
"license": "ISC" | ||
} | ||
} |
@@ -8,8 +8,8 @@ # RandAlThor-dropboxapi | ||
```javascript | ||
uploadLocal(location, options); | ||
uploadLocalSync(location, options); | ||
// Location is the file location of the image you wish to upload | ||
// (optional)options is a json object of type [FilesCommitInfo](https://dropbox.github.io/dropbox-sdk-js/global.html#FilesCommitInfo) | ||
downloadLocal(image, location, options); | ||
uploadBinarySync(data, options); | ||
// Data is the binary info of the image | ||
// (optional)options is a json object of type [FilesCommitInfo](https://dropbox.github.io/dropbox-sdk-js/global.html#FilesCommitInfo) | ||
downloadLocalSync(image, location, options); | ||
@@ -16,0 +16,0 @@ // image is the path of the image within dropbox |
3904
74