WorkDrive API - upload file

WorkDrive API - upload file

Hello Zoho Community,

I am new to to Zoho one and currently working on a nodeJS / Express integration with WorkDrive.

I am able to successfully ping various routes for getting teams / users / files etc but I cannot get a file upload to work correctly. 


I think its coming down to the content param
- is the multipart the individual file type? 'application/pdf' etc
- what is the actual structure of the body object needed to represent the actual content being passed?
 

Can somebody see where I am going wrong??
---
Part 1 - Create EJS template w/ variables for dynamic route
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>McCormick Client Upload Portal</title>
<link rel="stylesheet" href="/styles/uploadPortal.css" />
</head>
<body>
<div><%- include('../partials/header.ejs')%></div>
<h2><%= formTitle%></h2>
<div class="RequestInfo">
<div>Destination Folder ID: <%= parentID%></div>
<div>Requestor ID: <%= requestorID%></div>
</div>
<form
method="post"
id="uploadForm"
action="<%= `http://localhost:8000/Zoho/WorkDrive/uploadFile?parentID=${parentID}&requestorID=${requestorID}` %>"
enctype="multipart/form-data"
>
<input type="file"
name="fileUploader" multiple
/>
<input type="hidden"
id="parentID" name="parentID"
value="<%= parentID %>"
>
<input type="hidden"
id="requestorID" name="requestorID"
value="<%= requestorID %>"
>
<input type="submit"
name="submit" id="btnSubmit" value="Upload"
/>
</form>
<%- include('../partials/footer.ejs')%>
</body>
</html>

Part 2 - Use route to prompt user to upload files w/ EJS template
// GET - upload portal URL
router.get('/uploadPortal', function(req,res) {
/**
* We want to return the dynamic template with the following identifying information
*
* 1. 'parentID' => where is the file going in workdrive?
* 2. 'requestingID' => who is requesting the file
*/

// PARSE:
// `http://localhost:8000/Zoho/WorkDrive/uploadPortal?parentID=112233&requestorID=xxyyzz`
const parentID = req.query.parentID
// const parentID = '112233'
const requestorID = req.query.requestorID
// const requestorID = 'xxyyzz'

res.render(__dirname + '/views/uploadPortal', {
formTitle: 'McCormick Upload Portal',
parentID,
requestorID,
})
})

Part 3 - Accept user upload response
// POST - uploadFile
router.post('/uploadFile', function(req,res) {
let uploadType = undefined
const form = formidable({ multiples: true });
form.parse(req, (err, fields, files) => {
// CHECK: error
if (err) {
res.status(500).json("Upload Error 1")
// next(err)
// return
}

console.log('FIELDS', fields)

// PARSE: query
const { parentID, requestorID } = req.query
console.log(typeof files.fileUploader)

// CONFIGURE: upload type
if (Array.isArray(files.fileUploader)) {
uploadType = 'multiple'
res.status(500).json({"Error": "functionality coming soon"})
} else if (typeof files.fileUploader === 'object') {
uploadType = 'single'
} else {
res.status(500).json({ "Error": "Parsed Files: Unknown input type"})
}

// USE MODEL: upload all files to workdrive
model_WorkDrive.uploadFiles(req.tokens[0], uploadType, files.fileUploader, parentID, requestorID)
.then(results => {
res.status(200).json(results)
// res.status(200).json({ fields, files });
})
.catch(err => {
console.error(err)
res.status(500).json(err)
})
})
})

Part 3.5 - Shape of individual file
```
File {
  _events: [Object: null prototype] { error: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  size: 115540,
  path: '/var/folders/0k/t1ls81tn7sgby5p9zh5p68w40000gn/T/c07d33879b962b91c8b79ae00',
  name: 'Cover Letters 2.pdf',
  type: 'application/pdf',
  hash: null,
  lastModifiedDate: 2021-02-22T18:05:51.984Z,
...
...
```


Part 4 - Upload SINGLE file with WorkDrive API
* Desc:
* upload a single file to WorkDrive API
*
* Notes:
* this function will be used in a promise.all in order to faciliate
* uploading multiple files at once
*
* Args:
* parentID -- where to put the file
* requestorID -- which McCormick rep generated this upload request
*/

// UTILS
const getZohoAxios_WorkDrive = require('../../../axiosService/utils/getZohoAxios')

const uploadFile = async (ZOHO_ACCESS_TOKEN, baseObj, file) => {
console.log('-- INSIDE UPLOAD FILE --')
console.log('BASE OBJ: ', baseObj)
console.log(file)
console.log(process.env.ZOHO_accessToken_DELETETHIS)
// CREATE: axios instance
// const axios = getZohoAxios_WorkDrive.getZohoAxios_WorkDrive(ZOHO_ACCESS_TOKEN)
const axios = getZohoAxios_WorkDrive.getZohoAxios_WorkDrive(process.env.ZOHO_accessToken_DELETETHIS)
console.log(axios)

// PREP: queryParams
const params = `?filename=${file.name}&parent_id=${baseObj.parent_id}&override-name-exist=${baseObj.overrideNameExist}&type=${file.type}`
console.log(params)

// REQUEST:
// const result = await axios.post(`/api/v1/upload${params}`, body)
const result = await axios.post(`/api/v1/upload${params}`, {
"content": file
})
console.log('UPLOAD RESULT', result)
}

// EXPORTS
module.exports = uploadFile