Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(uploader): fix PUT content-type #1002

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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