Skip to content

Commit

Permalink
Redirect to doc detail after upload
Browse files Browse the repository at this point in the history
  • Loading branch information
svera committed Sep 24, 2024
1 parent 4669be8 commit 085ea87
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
10 changes: 5 additions & 5 deletions internal/index/bleve_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ import (
)

// AddFile adds a file to the index
func (b *BleveIndexer) AddFile(file string) error {
func (b *BleveIndexer) AddFile(file string) (string, error) {
ext := strings.ToLower(filepath.Ext(file))
if _, ok := b.reader[ext]; !ok {
return fmt.Errorf("file extension %s not supported", ext)
return "", fmt.Errorf("file extension %s not supported", ext)
}
meta, err := b.reader[ext].Metadata(file)
if err != nil {
return fmt.Errorf("error extracting metadata from file %s: %s", file, err)
return "", fmt.Errorf("error extracting metadata from file %s: %s", file, err)
}

document := b.createDocument(meta, file, nil)

err = b.idx.Index(document.ID, document)
if err != nil {
return fmt.Errorf("error indexing file %s: %s", file, err)
return "", fmt.Errorf("error indexing file %s: %s", file, err)
}
return nil
return document.Slug, nil
}

// RemoveFile removes a file from the index
Expand Down
2 changes: 1 addition & 1 deletion internal/webserver/controller/document/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type IdxReaderWriter interface {
SameSubjects(slug string, quantity int) ([]index.Document, error)
SameAuthors(slug string, quantity int) ([]index.Document, error)
SameSeries(slug string, quantity int) ([]index.Document, error)
AddFile(file string) error
AddFile(file string) (string, error)
RemoveFile(file string) error
Documents(IDs []string) (map[string]index.Document, error)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/webserver/controller/document/detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func (d *Controller) Detail(c *fiber.Ctx) error {
document = d.hlRepository.Highlighted(int(session.ID), document)
}

msg := ""
if c.Query("success") != "" {
msg = "Document uploaded successfully."
}

return c.Render("document", fiber.Map{
"Title": title,
"Document": document,
Expand All @@ -69,5 +74,6 @@ func (d *Controller) Detail(c *fiber.Ctx) error {
"SameAuthors": sameAuthors,
"SameSubjects": sameSubjects,
"WordsPerMinute": d.config.WordsPerMinute,
"Message": msg,
}, "layout")
}
15 changes: 7 additions & 8 deletions internal/webserver/controller/document/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ import (
"slices"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/valyala/fasthttp"
)

func (d *Controller) UploadForm(c *fiber.Ctx) error {
msg := ""
if c.Query("success") != "" {
msg = "Document uploaded successfully."
}

return c.Render("upload", fiber.Map{
"Title": "Coreander",
"Message": msg,
"MaxSize": d.config.UploadDocumentMaxSize,
}, "layout")
}
Expand Down Expand Up @@ -61,11 +56,13 @@ func (d *Controller) Upload(c *fiber.Ctx) error {

bytes, err := fileToBytes(file)
if err != nil {
log.Error()
return internalServerErrorStatus
}

destFile, err := d.appFs.Create(destination)
if err != nil {
log.Error(err)
return internalServerErrorStatus
}

Expand All @@ -74,12 +71,14 @@ func (d *Controller) Upload(c *fiber.Ctx) error {
}

destFile.Close()
if err := d.idx.AddFile(destination); err != nil {
slug, err := d.idx.AddFile(destination)
if err != nil {
log.Error(err)
os.Remove(destination)
return internalServerErrorStatus
}

return c.Redirect(fmt.Sprintf("/%s/upload?success=1", c.Params("lang")))
return c.Redirect(fmt.Sprintf("/%s/documents/%s?success=1", c.Params("lang"), slug))
}

func fileToBytes(fileHeader *multipart.FileHeader) ([]byte, error) {
Expand Down

0 comments on commit 085ea87

Please sign in to comment.