diff --git a/internal/index/bleve_write.go b/internal/index/bleve_write.go index 030ff15..fb848b9 100644 --- a/internal/index/bleve_write.go +++ b/internal/index/bleve_write.go @@ -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 diff --git a/internal/webserver/controller/document/controller.go b/internal/webserver/controller/document/controller.go index ca7b3d6..82bcf61 100644 --- a/internal/webserver/controller/document/controller.go +++ b/internal/webserver/controller/document/controller.go @@ -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) } diff --git a/internal/webserver/controller/document/detail.go b/internal/webserver/controller/document/detail.go index 4f28019..7ab8e03 100644 --- a/internal/webserver/controller/document/detail.go +++ b/internal/webserver/controller/document/detail.go @@ -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, @@ -69,5 +74,6 @@ func (d *Controller) Detail(c *fiber.Ctx) error { "SameAuthors": sameAuthors, "SameSubjects": sameSubjects, "WordsPerMinute": d.config.WordsPerMinute, + "Message": msg, }, "layout") } diff --git a/internal/webserver/controller/document/upload.go b/internal/webserver/controller/document/upload.go index 53f1fc6..d09cb2c 100644 --- a/internal/webserver/controller/document/upload.go +++ b/internal/webserver/controller/document/upload.go @@ -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") } @@ -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 } @@ -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) {