Skip to content

Commit

Permalink
fix: file filter not supported anymore on onedrive (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
Renji-XD authored Jul 10, 2024
1 parent d06cd8a commit ddcaf0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
31 changes: 27 additions & 4 deletions apps/web/src/lib/data/storage/handler/onedrive-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import pLimit from 'p-limit';

interface OneDriveFile extends ExternalFile {
thumbnails?: ExternalThumbnail[];
file: Record<string, string>;
folder: Record<string, string>;
}

interface BatchRequest {
Expand Down Expand Up @@ -302,7 +304,7 @@ export class OneDriveStorageHandler extends ApiStorageHandler {
params.append('select', `id,name`);

if (body) {
const { uploadUrl } = await this.request(
const { uploadUrl: uploadUrlResponse } = await this.request(
`${this.baseEndpoint}/${
remoteFile ? `${remoteFile.id}` : `${folderId}:/${encodeURIComponent(name)}:`
}/createUploadSession`,
Expand All @@ -317,6 +319,20 @@ export class OneDriveStorageHandler extends ApiStorageHandler {
}
);

const url = new URL(uploadUrlResponse);
const searchParams = [...url.searchParams];
const uploadUrl = `${url.origin}${url.pathname}`;

params.delete('select');

for (let index = 0, { length } = searchParams; index < length; index += 1) {
const [paramName, paramValue] = searchParams[index];

params.append(paramName, paramValue);
}

params.append('select', `id,name`);

if (!uploadUrl) {
throw new Error('Upload url was not returned');
}
Expand Down Expand Up @@ -389,8 +405,11 @@ export class OneDriveStorageHandler extends ApiStorageHandler {
} else {
const params = new URLSearchParams();

params.append('filter', listFiles ? 'file ne null' : 'folder ne null');
params.append('select', `id,name`);
params.append('select', `id,name,file,folder`);

if (!listFiles) {
params.append('filter', 'folder ne null');
}

if (withThumbnail) {
params.append('expand', `thumbnails`);
Expand All @@ -400,7 +419,11 @@ export class OneDriveStorageHandler extends ApiStorageHandler {
}

if (response) {
files.push(...(response.value || []));
files.push(
...(response.value || []).filter(
(item: OneDriveFile) => (listFiles && !!item.file) || (!listFiles && !!item.folder)
)
);

if (response['@odata.nextLink']) {
await this.list(parent, withThumbnail, listFiles, files, response?.['@odata.nextLink']);
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
return;
}
window.opener.postMessage(
/* window.opener.postMessage(
{
type: 'failure',
payload: {
Expand All @@ -135,7 +135,7 @@
}
},
origin
);
); */
}
function checkAuthResponse(
Expand Down

0 comments on commit ddcaf0c

Please sign in to comment.