Skip to content

Commit

Permalink
Merge pull request #935 from nextcloud-libraries/artonge/feat/allow_c…
Browse files Browse the repository at this point in the history
…ustom_headers

Set proper headers in upload requests
  • Loading branch information
artonge authored Oct 11, 2023
2 parents 991f720 + 5900f74 commit abab125
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
23 changes: 21 additions & 2 deletions lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,17 @@ export class Uploader {

// Init request queue
const request = () => {
return uploadData(`${tempUrl}/${chunk+1}`, blob, upload.signal, () => this.updateStats(), destinationFile)
return uploadData(
`${tempUrl}/${chunk+1}`,
blob,
upload.signal,
() => this.updateStats(),
destinationFile,
{
'X-OC-Mtime': file.lastModified,
'OC-Total-Length': file.size,
}
)
// Update upload progress on chunk completion
.then(() => { upload.uploaded = upload.uploaded + maxChunkSize })
.catch((error) => {
Expand Down Expand Up @@ -261,7 +271,16 @@ export class Uploader {
const blob = await getChunk(file, 0, upload.size)
const request = async () => {
try {
upload.response = await uploadData(destinationFile, blob, upload.signal, () => this.updateStats())
upload.response = await uploadData(
destinationFile,
blob,
upload.signal,
() => this.updateStats(),
undefined,
{
'X-OC-Mtime': file.lastModified,
}
)

// Update progress
upload.uploaded = upload.size
Expand Down
7 changes: 5 additions & 2 deletions lib/utils/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type UploadData = Blob | (() => Promise<Blob>)
/**
* Upload some data to a given path
*/
export const uploadData = async function(url: string, uploadData: UploadData, signal: AbortSignal, onUploadProgress = () => {}, destinationFile: string | undefined = undefined): Promise<AxiosResponse> {
export const uploadData = async function(url: string, uploadData: UploadData, signal: AbortSignal, onUploadProgress = () => {}, destinationFile: string | undefined = undefined, headers: any = undefined): Promise<AxiosResponse> {
let data: Blob

if (uploadData instanceof Blob) {
Expand All @@ -21,7 +21,10 @@ export const uploadData = async function(url: string, uploadData: UploadData, si
data = await uploadData()
}

const headers = destinationFile ? { Destination: destinationFile } : undefined
if (destinationFile) {
headers ??= {}
headers.Destination = destinationFile
}

return await axios.request({
method: 'PUT',
Expand Down

0 comments on commit abab125

Please sign in to comment.