Last active
December 19, 2017 19:10
-
-
Save montrealist/ce1b773742dbe6fed1ab653700c94df6 to your computer and use it in GitHub Desktop.
box-node-sdk: search for file first and if it exists upload new version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fileName = 'something.txt'; | |
client.search.query ( | |
fileName, { limit: 1 }, | |
function(error, data) { | |
if ( | |
data.total_count && | |
data.total_count > 0 && | |
data.entries[0].name === fileName | |
) { | |
// have a match - upload new version | |
client.files.uploadNewFileVersion( | |
data.entries[0].id, | |
stream, | |
function(error, data) { /* callback */ } | |
); | |
} else { | |
// no match - upload new file | |
client.files.preflightUploadFile( | |
id, | |
{ name: fileName }, | |
null, | |
function(error) { | |
client.files.uploadFile( | |
id, | |
fileName, | |
stream, | |
function(error, data) { /* callback */ } | |
); | |
} | |
); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment