Skip to content

Commit d03e7d0

Browse files
authored
fix: word: fix file listing (#508)
Signed-off-by: Grant Linville <[email protected]>
1 parent eaae3f6 commit d03e7d0

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

word/pkg/commands/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func ListDocs(ctx context.Context) error {
2121
return fmt.Errorf("failed to list word docs: %w", err)
2222
}
2323

24+
if len(infos) == 0 {
25+
fmt.Println("No word docs found")
26+
return nil
27+
}
28+
2429
gptscriptClient, err := gptscript.NewGPTScript()
2530
if err != nil {
2631
return fmt.Errorf("failed to create GPTScript client: %w", err)

word/pkg/graph/docs.go

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"code.sajari.com/docconv/v2"
1212
kiota "github.com/microsoft/kiota-abstractions-go"
1313
msgraphsdkgo "github.com/microsoftgraph/msgraph-sdk-go"
14-
"github.com/microsoftgraph/msgraph-sdk-go/drives"
1514
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
1615
"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
1716
)
@@ -222,37 +221,77 @@ func CreateDoc(ctx context.Context, client *msgraphsdkgo.GraphServiceClient, nam
222221
return name, deref(uploadedItem.GetId()), nil
223222
}
224223

224+
// ListDocs lists all documents in the user's OneDrive.
225+
// It recursively traverses folders to find all documents.
225226
func ListDocs(ctx context.Context, c *msgraphsdkgo.GraphServiceClient) ([]DocInfo, error) {
226227
drive, err := c.Me().Drive().Get(ctx, nil)
227228
if err != nil {
228-
return nil, err
229+
return nil, fmt.Errorf("failed to get drive: %w", err)
229230
}
230231

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().
238234
ByDriveId(deref(drive.GetId())).
239-
SearchWithQ(ptr("docx")).
240-
GetAsSearchWithQGetResponse(ctx, opts)
235+
Root().
236+
Get(ctx, nil)
241237
if err != nil {
242-
return nil, err
238+
return nil, fmt.Errorf("failed to get root folder: %w", err)
243239
}
244240

245241
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)
251245
}
252246

253247
return infos, nil
254248
}
255249

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+
256295
func GetDocByPath(ctx context.Context, c *msgraphsdkgo.GraphServiceClient, path string) (string, error) {
257296
drive, err := c.Me().Drive().Get(ctx, nil)
258297
if err != nil {

0 commit comments

Comments
 (0)