File size: 1,235 Bytes
f866820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Dropbox API utilities for file access
 * Privacy: Files are fetched via backend proxy, processed client-side
 */

import { getDropboxFile } from './client';

/**
 * Fetch file content from Dropbox via backend proxy
 */
export async function fetchFileContent(filePath, accessToken) {
  const result = await getDropboxFile(filePath, accessToken);

  if (result.error) {
    throw new Error(result.error);
  }

  return result.content;
}

/**
 * Process selected files from Dropbox picker
 * Fetches content for each file and returns array of {name, content}
 */
export async function processSelectedFiles(files, accessToken, onProgress) {
  const results = [];

  for (let i = 0; i < files.length; i++) {
    const file = files[i];
    onProgress?.({ fileName: file.name, current: i + 1, total: files.length });

    try {
      const content = await fetchFileContent(file.path_lower, accessToken);
      if (content) {
        results.push({
          id: file.id,
          name: file.name,
          path: file.path_lower, // Store path for re-fetching at query time
          content: content
        });
      }
    } catch (error) {
      console.error(`Failed to read ${file.name}:`, error);
    }
  }

  return results;
}