@@ -11,7 +11,6 @@ import (
11
11
"code.sajari.com/docconv/v2"
12
12
kiota "github.com/microsoft/kiota-abstractions-go"
13
13
msgraphsdkgo "github.com/microsoftgraph/msgraph-sdk-go"
14
- "github.com/microsoftgraph/msgraph-sdk-go/drives"
15
14
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
16
15
"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
17
16
)
@@ -222,37 +221,77 @@ func CreateDoc(ctx context.Context, client *msgraphsdkgo.GraphServiceClient, nam
222
221
return name , deref (uploadedItem .GetId ()), nil
223
222
}
224
223
224
+ // ListDocs lists all documents in the user's OneDrive.
225
+ // It recursively traverses folders to find all documents.
225
226
func ListDocs (ctx context.Context , c * msgraphsdkgo.GraphServiceClient ) ([]DocInfo , error ) {
226
227
drive , err := c .Me ().Drive ().Get (ctx , nil )
227
228
if err != nil {
228
- return nil , err
229
+ return nil , fmt . Errorf ( "failed to get drive: %w" , err )
229
230
}
230
231
231
- opts := & drives.ItemSearchWithQRequestBuilderGetRequestConfiguration {
232
- QueryParameters : & drives.ItemSearchWithQRequestBuilderGetQueryParameters {
233
- // Request that these fields are returned in the response.
234
- Select : []string {"id" , "name" , "parentReference" },
235
- },
236
- }
237
- docs , err := c .Drives ().
232
+ // Start from the root folder
233
+ root , err := c .Drives ().
238
234
ByDriveId (deref (drive .GetId ())).
239
- SearchWithQ ( ptr ( "docx" ) ).
240
- GetAsSearchWithQGetResponse (ctx , opts )
235
+ Root ( ).
236
+ Get (ctx , nil )
241
237
if err != nil {
242
- return nil , err
238
+ return nil , fmt . Errorf ( "failed to get root folder: %w" , err )
243
239
}
244
240
245
241
var infos []DocInfo
246
- for _ , info := range docs .GetValue () {
247
- infos = append (infos , DocInfo {
248
- ID : deref (info .GetId ()),
249
- Name : deref (info .GetName ()),
250
- })
242
+ err = listDocsInFolder (ctx , c , deref (drive .GetId ()), deref (root .GetId ()), & infos )
243
+ if err != nil {
244
+ return nil , fmt .Errorf ("failed to list docs: %w" , err )
251
245
}
252
246
253
247
return infos , nil
254
248
}
255
249
250
+ // listDocsInFolder recursively lists all documents in a folder and its subfolders.
251
+ func listDocsInFolder (ctx context.Context , c * msgraphsdkgo.GraphServiceClient , driveID , folderID string , infos * []DocInfo ) error {
252
+ items , err := c .Drives ().
253
+ ByDriveId (driveID ).
254
+ Items ().
255
+ ByDriveItemId (folderID ).
256
+ Children ().
257
+ Get (ctx , nil )
258
+ if err != nil {
259
+ return fmt .Errorf ("failed to get items in folder: %w" , err )
260
+ }
261
+
262
+ // Process this page of items
263
+ for _ , item := range items .GetValue () {
264
+ // Skip folders, but process their contents
265
+ if item .GetFolder () != nil {
266
+ err = listDocsInFolder (ctx , c , driveID , deref (item .GetId ()), infos )
267
+ if err != nil {
268
+ return err
269
+ }
270
+ continue
271
+ }
272
+
273
+ // Only include Word documents
274
+ file := item .GetFile ()
275
+ if file == nil || ! isWordDocument (deref (item .GetName ())) {
276
+ continue
277
+ }
278
+
279
+ // Add Word documents to our list
280
+ * infos = append (* infos , DocInfo {
281
+ ID : deref (item .GetId ()),
282
+ Name : deref (item .GetName ()),
283
+ })
284
+ }
285
+
286
+ return nil
287
+ }
288
+
289
+ // isWordDocument checks if a file is a Microsoft Word document based on its extension.
290
+ func isWordDocument (filename string ) bool {
291
+ ext := strings .ToLower (filepath .Ext (filename ))
292
+ return ext == ".docx" || ext == ".doc"
293
+ }
294
+
256
295
func GetDocByPath (ctx context.Context , c * msgraphsdkgo.GraphServiceClient , path string ) (string , error ) {
257
296
drive , err := c .Me ().Drive ().Get (ctx , nil )
258
297
if err != nil {
0 commit comments