Skip to content

Commit

Permalink
Merge pull request #1002 from nextcloud-libraries/fix/content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Dec 15, 2023
2 parents 9c927b0 + 2c613f0 commit 5847d13
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
7 changes: 7 additions & 0 deletions __tests__/utils/upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ describe('Upload data', () => {
data: blob,
signal,
onUploadProgress,
headers: {
'Content-Type': 'application/octet-stream',
},
})
})
test('Upload async data stream', async () => {
Expand All @@ -139,6 +142,9 @@ describe('Upload data', () => {
data: blob,
signal,
onUploadProgress,
headers: {
'Content-Type': 'application/octet-stream',
},
})
})

Expand All @@ -161,6 +167,7 @@ describe('Upload data', () => {
onUploadProgress,
headers: {
Destination: url,
'Content-Type': 'application/octet-stream',
},
})
})
Expand Down
2 changes: 2 additions & 0 deletions lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class Uploader {
{
'X-OC-Mtime': file.lastModified / 1000,
'OC-Total-Length': file.size,
'Content-Type': 'application/octet-stream',
},
)
// Update upload progress on chunk completion
Expand Down Expand Up @@ -284,6 +285,7 @@ export class Uploader {
undefined,
{
'X-OC-Mtime': file.lastModified / 1000,
'Content-Type': file.type,
},
)

Expand Down
24 changes: 22 additions & 2 deletions lib/utils/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,41 @@ type UploadData = Blob | (() => Promise<Blob>)

/**
* Upload some data to a given path
* @param url the url to upload to
* @param uploadData the data to upload
* @param signal the abort signal
* @param onUploadProgress the progress callback
* @param destinationFile the final destination file (often used for chunked uploads)
* @param headers additional headers
*/
export const uploadData = async function(url: string, uploadData: UploadData, signal: AbortSignal, onUploadProgress = () => {}, destinationFile: string | undefined = undefined, headers: any = undefined): Promise<AxiosResponse> {
export const uploadData = async function(
url: string,
uploadData: UploadData,
signal: AbortSignal,
onUploadProgress = () => {},
destinationFile: string | undefined = undefined,
headers: Record<string, string|number> = {},
): Promise<AxiosResponse> {
let data: Blob

// If the upload data is a blob, we can directly use it
// Otherwise, we need to wait for the promise to resolve
if (uploadData instanceof Blob) {
data = uploadData
} else {
data = await uploadData()
}

// Helps the server to know what to do with the file afterwards (e.g. chunked upload)
if (destinationFile) {
headers ??= {}
headers.Destination = destinationFile
}

// If no content type is set, we default to octet-stream
if (!headers['Content-Type']) {
headers['Content-Type'] = 'application/octet-stream'
}

return await axios.request({
method: 'PUT',
url,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"cypress": "cypress run --component",
"cypress:gui": "cypress open --component",
"test": "vitest run",
"test:watch": "vitest run --watchAll --verbose true",
"test:watch": "vitest run --watch",
"test:coverage": "vitest run --coverage",
"l10n:extract": "node build/extract-l10n.js"
},
Expand Down

0 comments on commit 5847d13

Please sign in to comment.