File size: 6,263 Bytes
f0743f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | import { useMutation } from '@tanstack/react-query';
import type { UseMutationResult } from '@tanstack/react-query';
export interface SharePointFile {
id: string;
name: string;
size: number;
webUrl: string;
downloadUrl: string;
driveId: string;
itemId: string;
sharePointItem: any;
}
export interface SharePointDownloadProgress {
fileId: string;
fileName: string;
loaded: number;
total: number;
progress: number;
}
export interface SharePointBatchProgress {
completed: number;
total: number;
currentFile?: string;
failed: string[];
}
export const useSharePointFileDownload = (): UseMutationResult<
File,
unknown,
{
file: SharePointFile;
accessToken: string;
onProgress?: (progress: SharePointDownloadProgress) => void;
}
> => {
return useMutation({
mutationFn: async ({ file, accessToken, onProgress }) => {
const downloadUrl =
file.downloadUrl ||
`https://graph.microsoft.com/v1.0/drives/${file.driveId}/items/${file.itemId}/content`;
const response = await fetch(downloadUrl, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (!response.ok) {
throw new Error(`Download failed: ${response.status} ${response.statusText}`);
}
const contentLength = parseInt(response.headers.get('content-length') || '0');
const reader = response.body?.getReader();
if (!reader) {
throw new Error('Failed to get response reader');
}
const chunks: Uint8Array[] = [];
let receivedLength = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
receivedLength += value.length;
if (onProgress) {
onProgress({
fileId: file.id,
fileName: file.name,
loaded: receivedLength,
total: contentLength || file.size,
progress: Math.round((receivedLength / (contentLength || file.size)) * 100),
});
}
}
const allChunks = new Uint8Array(receivedLength);
let position = 0;
for (const chunk of chunks) {
allChunks.set(chunk, position);
position += chunk.length;
}
const contentType =
response.headers.get('content-type') || getMimeTypeFromFileName(file.name);
const blob = new Blob([allChunks], { type: contentType });
const downloadedFile = new File([blob], file.name, {
type: contentType,
lastModified: Date.now(),
});
return downloadedFile;
},
retry: 2,
});
};
export const useSharePointBatchDownload = (): UseMutationResult<
File[],
unknown,
{
files: SharePointFile[];
accessToken: string;
onProgress?: (progress: SharePointBatchProgress) => void;
},
unknown
> => {
return useMutation({
mutationFn: async ({ files, accessToken, onProgress }) => {
const downloadedFiles: File[] = [];
const failed: string[] = [];
let completed = 0;
const concurrencyLimit = 3;
const chunks: SharePointFile[][] = [];
for (let i = 0; i < files.length; i += concurrencyLimit) {
chunks.push(files.slice(i, i + concurrencyLimit));
}
for (const chunk of chunks) {
const chunkPromises = chunk.map(async (file) => {
try {
const downloadUrl =
file.downloadUrl ||
`https://graph.microsoft.com/v1.0/drives/${file.driveId}/items/${file.itemId}/content`;
const response = await fetch(downloadUrl, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}
const blob = await response.blob();
const contentType =
response.headers.get('content-type') || getMimeTypeFromFileName(file.name);
const downloadedFile = new File([blob], file.name, {
type: contentType,
lastModified: Date.now(),
});
completed++;
onProgress?.({
completed,
total: files.length,
currentFile: file.name,
failed,
});
return downloadedFile;
} catch (error) {
console.error(`Failed to download ${file.name}:`, error);
failed.push(file.name);
completed++;
onProgress?.({
completed,
total: files.length,
currentFile: `Error: ${file.name}`,
failed,
});
throw error;
}
});
const chunkResults = await Promise.allSettled(chunkPromises);
chunkResults.forEach((result) => {
if (result.status === 'fulfilled') {
downloadedFiles.push(result.value);
}
});
}
if (failed.length > 0) {
console.warn(`Failed to download ${failed.length} files:`, failed);
}
return downloadedFiles;
},
retry: 1,
});
};
function getMimeTypeFromFileName(fileName: string): string {
const extension = fileName.split('.').pop()?.toLowerCase();
const mimeTypes: Record<string, string> = {
// Documents
pdf: 'application/pdf',
doc: 'application/msword',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
xls: 'application/vnd.ms-excel',
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
ppt: 'application/vnd.ms-powerpoint',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
txt: 'text/plain',
csv: 'text/csv',
// Images
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png',
gif: 'image/gif',
bmp: 'image/bmp',
svg: 'image/svg+xml',
webp: 'image/webp',
// Archives
zip: 'application/zip',
rar: 'application/x-rar-compressed',
// Media
mp4: 'video/mp4',
mp3: 'audio/mpeg',
wav: 'audio/wav',
};
return mimeTypes[extension || ''] || 'application/octet-stream';
}
export { getMimeTypeFromFileName };
|