Replace dsc import calls with Docspell API
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -35,7 +36,7 @@ type DocspellItem struct {
|
||||
Direction string `json:"direction"`
|
||||
State string `json:"state"`
|
||||
Created int64 `json:"created"`
|
||||
ItemDate int64 `json:"item_date"`
|
||||
ItemDate int64 `json:"itemDate"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -164,8 +165,9 @@ func runImportCommand(cfg config, password string, args []string) {
|
||||
|
||||
validateConfig(cfg)
|
||||
|
||||
loginCmd := exec.Command("dsc", "login", "--user", cfg.User, "--password", password)
|
||||
if err := loginCmd.Run(); err != nil {
|
||||
ctx := context.Background()
|
||||
client := NewDocspellClient(cfg.DocspellURL, defaultDocspellAuthHeader)
|
||||
if err := client.Login(ctx, cfg.User, password); err != nil {
|
||||
fmt.Println("Login failed:", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
@@ -201,21 +203,14 @@ func runImportCommand(cfg config, password string, args []string) {
|
||||
fmt.Println()
|
||||
fmt.Printf("%s:\n", info.Name())
|
||||
|
||||
cmd := exec.Command("dsc", "-f", "json", "file-exists", path)
|
||||
output, err := cmd.Output()
|
||||
fileExistsResponse, err := client.FileExists(ctx, path)
|
||||
if err != nil {
|
||||
fmt.Printf(" ERROR %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
var fileExistsResponse []FileExistsResult
|
||||
if err := json.Unmarshal(output, &fileExistsResponse); err != nil {
|
||||
fmt.Printf(" ERROR parsing response: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(fileExistsResponse) > 0 && fileExistsResponse[0].Exists {
|
||||
err := handleExistingFile(cfg, fileExistsResponse[0])
|
||||
if fileExistsResponse.Exists {
|
||||
err := handleExistingFile(ctx, client, cfg, fileExistsResponse)
|
||||
if err != nil {
|
||||
fmt.Println(" ERROR", err)
|
||||
}
|
||||
@@ -223,23 +218,15 @@ func runImportCommand(cfg config, password string, args []string) {
|
||||
fmt.Println(" Files does not exist, yet")
|
||||
if options.uploadMissing {
|
||||
fmt.Print(" ...uploading file..")
|
||||
cmd = exec.Command("dsc", "-f", "json", "upload", path)
|
||||
output, err := cmd.Output()
|
||||
uploadResult, err := client.UploadFile(ctx, path, UploadMeta{Multiple: true})
|
||||
if err != nil {
|
||||
fmt.Printf("\n ERROR uploading: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
var uploadResult map[string]interface{}
|
||||
if err := json.Unmarshal(output, &uploadResult); err != nil {
|
||||
fmt.Printf("\n ERROR parsing upload result: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if uploadResult["success"].(bool) {
|
||||
if uploadResult.Success {
|
||||
fmt.Println(". done")
|
||||
} else {
|
||||
fmt.Printf("\n ERROR %v\n", uploadResult)
|
||||
fmt.Printf("\n ERROR %s\n", uploadResult.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -268,25 +255,22 @@ func parseImportOptions(args []string) (importOptions, error) {
|
||||
return options, parser.ParseArgs(args)
|
||||
}
|
||||
|
||||
func handleExistingFile(cfg config, fileExistsResponse FileExistsResult) error {
|
||||
func handleExistingFile(ctx context.Context, client *DocspellClient, cfg config, fileExistsResponse FileExistsResult) error {
|
||||
// File exists in Docspell
|
||||
items := fileExistsResponse.Items
|
||||
if len(items) == 0 {
|
||||
return fmt.Errorf("file exists response contained no items")
|
||||
}
|
||||
item := items[0]
|
||||
itemID := item.ID
|
||||
itemName := item.Name
|
||||
|
||||
// Get item details
|
||||
cmd := exec.Command("dsc", "-f", "json", "item", "get", itemID)
|
||||
output, err := cmd.Output()
|
||||
itemDetails, err := client.GetItem(ctx, itemID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get item details: %w", err)
|
||||
}
|
||||
|
||||
var itemDetails DocspellItemDetails
|
||||
if err := json.Unmarshal(output, &itemDetails); err != nil {
|
||||
return fmt.Errorf("parse item details: %w", err)
|
||||
}
|
||||
|
||||
folder := "null"
|
||||
if itemDetails.Folder != nil {
|
||||
folder = itemDetails.Folder.Name
|
||||
@@ -294,10 +278,10 @@ func handleExistingFile(cfg config, fileExistsResponse FileExistsResult) error {
|
||||
extension := filepath.Ext(fileExistsResponse.File)[1:]
|
||||
|
||||
var corr string
|
||||
if itemDetails.CorrespondingOrganisation != nil && itemDetails.CorrespondingOrganisation.Name != "" {
|
||||
corr = itemDetails.CorrespondingOrganisation.Name
|
||||
} else if itemDetails.CorrespondingPerson != nil && itemDetails.CorrespondingPerson.Name != "" {
|
||||
corr = itemDetails.CorrespondingPerson.Name
|
||||
if itemDetails.CorrOrg != nil && itemDetails.CorrOrg.Name != "" {
|
||||
corr = itemDetails.CorrOrg.Name
|
||||
} else if itemDetails.CorrPerson != nil && itemDetails.CorrPerson.Name != "" {
|
||||
corr = itemDetails.CorrPerson.Name
|
||||
}
|
||||
fmt.Printf(" File already exists: %s\n", itemName)
|
||||
fmt.Printf(" URL: %s/app/item/%s\n", cfg.DocspellURL, itemID)
|
||||
@@ -345,13 +329,6 @@ func validateConfig(cfg config) {
|
||||
}
|
||||
}
|
||||
|
||||
type DocspellItemDetails struct {
|
||||
DocspellItem
|
||||
CorrespondingOrganisation *DocspellEntity `json:"corr-org"`
|
||||
CorrespondingPerson *DocspellEntity `json:"corr-person"`
|
||||
Folder *DocspellEntity `json:"folder"`
|
||||
}
|
||||
|
||||
type DocspellEntity struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
||||
Reference in New Issue
Block a user