Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
postgres:
image: postgres
volumes:
- pg-data:/var/lib/postgresql/data
- pg-data:/var/lib/postgresql
environment:
POSTGRES_USER: 'user'
POSTGRES_PASSWORD: 'pass'
Expand All @@ -18,6 +18,7 @@ services:
volumes:
- ./data:/data
environment:
KOMPANION_BSTORAGE_TYPE: postgres
KOMPANION_PG_URL: 'postgres://user:pass@postgres:5432/postgres'
KOMPANION_BSTORAGE_PATH: '/data/books/'
KOMPANION_AUTH_USERNAME: 'user'
Expand All @@ -28,4 +29,4 @@ services:
- postgres

volumes:
pg-data:
pg-data:
14 changes: 14 additions & 0 deletions internal/controller/http/web/books.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func newBooksRoutes(handler *gin.RouterGroup, shelf library.Shelf, stats stats.R
handler.POST("/:bookID", r.updateBookMetadata)
handler.GET("/:bookID/download", r.downloadBook)
handler.GET("/:bookID/cover", r.viewBookCover)
handler.POST("/:bookID/delete", r.deleteBook)
}

func (r *booksRoutes) listBooks(c *gin.Context) {
Expand Down Expand Up @@ -203,3 +204,16 @@ func (r *booksRoutes) viewBookCover(c *gin.Context) {
}
c.File(cover.Name())
}

func (r *booksRoutes) deleteBook(c *gin.Context) {
bookID := c.Param("bookID")

err := r.shelf.DeleteBook(c, bookID)
if err != nil {
r.logger.Error(err, "http - v1 - shelf - deleteBook")
c.JSON(500, passStandartContext(c, gin.H{"message": "internal server error"}))
return
}

c.Redirect(302, "/books")
}
21 changes: 21 additions & 0 deletions internal/library/book_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ func (bdr *BookDatabaseRepo) Update(ctx context.Context, book entity.Book) error
return nil
}

// Delete -. only delete from database
func (bdr *BookDatabaseRepo) Delete(ctx context.Context, book entity.Book) error {
sql := `
DELETE FROM library_book
WHERE id = $1
`

args := []interface{}{
book.ID,
}

rows, err := bdr.Pool.Exec(ctx, sql, args...)
if err != nil {
return fmt.Errorf("BookDatabaseRepo - Delete - r.Pool.Exec: %w", err)
}
if rows.RowsAffected() == 0 {
return fmt.Errorf("BookDatabaseRepo - Delete - no rows affected")
}
return nil
}

// List -. only select from database
func (bdr *BookDatabaseRepo) List(ctx context.Context,
sortBy, sortOrder string,
Expand Down
2 changes: 2 additions & 0 deletions internal/library/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
DownloadBook(ctx context.Context, bookID string) (entity.Book, *os.File, error)
UpdateBookMetadata(ctx context.Context, bookID string, metadata entity.Book) (entity.Book, error)
ViewCover(ctx context.Context, bookID string) (*os.File, error)
DeleteBook(ctx context.Context, bookID string) error
}

// BookRepo -.
Expand All @@ -32,5 +33,6 @@ type (
GetById(context.Context, string) (entity.Book, error)
GetByFileHash(context.Context, string) (entity.Book, error)
Update(context.Context, entity.Book) error
Delete(context.Context, entity.Book) error
}
)
39 changes: 39 additions & 0 deletions internal/library/shelf.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,30 @@ func (uc *BookShelf) UpdateBookMetadata(ctx context.Context, bookID string, meta
return updatedBook, nil
}

func (uc *BookShelf) DeleteBook(ctx context.Context, bookID string) error {
book, err := uc.repo.GetById(ctx, bookID)
if err != nil {
return fmt.Errorf("BookShelf - DeleteBook - s.repo.Get: %w", err)
}

err = uc.deleteCover(ctx, bookID)
if err != nil {
return fmt.Errorf("BookShelf - deleteCover - s.repo.Delete: %w", err)
}

err = uc.repo.Delete(ctx, book)
if err != nil {
return fmt.Errorf("BookShelf - DeleteBook - s.repo.Delete: %w", err)
}

err = uc.storage.Delete(ctx, book.FilePath)
if err != nil {
return fmt.Errorf("BookShelf - DeleteBook - s.storage.Delete: %w", err)
}

return nil
}

func (uc *BookShelf) DownloadBook(ctx context.Context, bookID string) (entity.Book, *os.File, error) {
book, err := uc.repo.GetById(ctx, bookID)
if err != nil {
Expand Down Expand Up @@ -198,3 +222,18 @@ func writeCover(
}
return coverpath, nil
}

func (uc *BookShelf) deleteCover(ctx context.Context, bookID string) error {
book, err := uc.repo.GetById(ctx, bookID)
if err != nil {
return fmt.Errorf("BookShelf - deleteCover - s.repo.Get: %w", err)
}
if book.CoverPath == "" {
return nil
}
err = uc.storage.Delete(ctx, book.CoverPath)
if err != nil {
return fmt.Errorf("BookShelf - deleteCover - s.Storage.Delete: %w", err)
}
return nil
}
9 changes: 9 additions & 0 deletions internal/storage/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func (s *FilesystemStorage) Write(ctx context.Context, src, dest string) error {
return nil
}

func (s *FilesystemStorage) Delete(ctx context.Context, p string) error {
filepath := path.Join(s.root, p)
err := os.Remove(filepath)
if err != nil {
return err
}
return nil
}

func checkSystemWrites(root string) error {
// Create a temporary file in the root directory
tempFile, err := os.CreateTemp(root, "write_test")
Expand Down
12 changes: 12 additions & 0 deletions internal/storage/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestFilesystemStorage(t *testing.T) {
if err != nil {
t.Errorf("Error reading file: %v", err)
}

defer readFile.Close()
readBody, err := os.ReadFile(readFile.Name())
if err != nil {
Expand All @@ -50,4 +51,15 @@ func TestFilesystemStorage(t *testing.T) {
if string(readBody) != string(body) {
t.Errorf("Expected body %s, got %s", string(body), string(readBody))
}

err = st.Delete(ctx, "test")
if err != nil {
t.Errorf("Error deleting file: %v", err)
}

defer readFile.Close()
_, err =os.ReadFile(readFile.Name())
if err == nil {
t.Errorf("Error deleting file failed")
}
}
3 changes: 2 additions & 1 deletion internal/storage/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import (
type Storage interface {
Write(ctx context.Context, source string, filepath string) error
Read(ctx context.Context, filepath string) (*os.File, error)
}
Delete(ctx context.Context, filepath string) error
}
7 changes: 7 additions & 0 deletions internal/storage/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ func (s *MemoryStorage) Write(ctx context.Context, source string, filepath strin
s.mu.Unlock()
return nil
}

func (s *MemoryStorage) Delete(ctx context.Context, filepath string) error {
s.mu.Lock()
s.data[filepath] = nil
s.mu.Unlock()
return nil
}
14 changes: 14 additions & 0 deletions internal/storage/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ func (ps *PostgresStorage) Read(ctx context.Context, filepath string) (*os.File,
}
return tempFile, nil
}

func (ps *PostgresStorage) Delete(ctx context.Context, filepath string) error {
sql := `
DELETE FROM storage_blob
WHERE file_path = $1
`
args := []interface{}{filepath}
_, err := ps.Pool.Exec(ctx, sql, args...)
if err != nil {
return fmt.Errorf("PostgresStorage - Read - r.Pool.QueryRow: %w", err)
}

return nil
}
136 changes: 111 additions & 25 deletions pkg/metadata/pdf.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package metadata

import (
"bufio"
"os"
"bytes"
"unicode/utf8"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
"regexp"
"strings"
"io"
)

// PDFMetadata holds the extracted PDFmetadata information
Expand All @@ -16,47 +21,128 @@ type PDFMetadata struct {

// extractPDFMetadataFromHeader scans the first part of the PDF file for PDFmetadata information
func extractPdfMetadata(tmpFile *os.File) (Metadata, error) {
scanner := bufio.NewScanner(tmpFile)
var PDFmetadata Metadata
for scanner.Scan() {
line := scanner.Text()

if strings.Contains(line, "/Title") {
PDFmetadata.Title = extractValue(line, "/Title")
const chunksize = 64 * 1024
const tailsize = 1024

buf := make([]byte, chunksize)
tail := make([]byte, tailsize)

for {
n, err := tmpFile.Read(buf)

block := append(tail, buf[:n]...)

if bytes.Contains(block, []byte("/Title")) {
PDFmetadata.Title = clean(extractValue(block, "/Title"))

}

if bytes.Contains(block, []byte("/Author")) {
PDFmetadata.Author = clean(extractValue(block, "/Author"))
}
if strings.Contains(line, "/Author") {
PDFmetadata.Author = extractValue(line, "/Author")

if err == io.EOF {
break
}

if err != nil {
return Metadata{}, err
}
// if strings.Contains(line, "/Subject") {
// PDFmetadata.Subject = extractValue(line, "/Subject")
// }
// if strings.Contains(line, "/Keywords") {
// PDFmetadata.Keywords = extractValue(line, "/Keywords")
// }

// Break early if we've found all fields
if PDFmetadata.Title != "" && PDFmetadata.Author != "" {
break
}
}

if err := scanner.Err(); err != nil {
return Metadata{}, err
// use file name if no title found
if PDFmetadata.Title == "" {
parts := strings.Split(tmpFile.Name(), "/")
parts = strings.Split(parts[len(parts) - 1], ".")
PDFmetadata.Title = parts[0]
}

return PDFmetadata, nil
}

// extractValue extracts the value for a specific metadata field
func extractValue(line string, field string) string {
start := strings.Index(line, field+"(")
if start == -1 {
return ""
func extractValue(block []byte, field string) string {
fieldIndex := bytes.Index(block, []byte(field))
start := bytes.Index(block[fieldIndex:], []byte("(")) + fieldIndex + 1
end := findLiteralEnd(block[start:]) + start
value := block[start:end]

if utf8.Valid(value) {
return string(value)
}

// remove escape symbol
regex := regexp.MustCompile(`\\(.)`)
value = regex.ReplaceAll(value, []byte("$1"))

// UTF16BE
if len(value) >= 2 && value[0] == 0xFE && value[1] == 0xFF {
return decodeUTF16("be", value)
}
// UTF16LE
if len(value) >= 2 && value[0] == 0xFF && value[1] == 0xFE {
return decodeUTF16("le", value)
}

return ""
}

// find the literal end, excluding parentheis in the field
func findLiteralEnd(b []byte) int {
depth := 1
escaped := false
for i := 0; i < len(b); i++ {
c := b[i]
if escaped {
escaped = false
continue
}
if c == '\\' {
escaped = true
continue
}
if c == '(' {
depth++
continue
}
if c == ')' {
depth--
if depth == 0 {
return i
}
}
}
start += len(field) + 1 // Skip past the field and the opening parenthesis
end := strings.Index(line[start:], ")")
if end == -1 {
return ""
return -1
}

// decode UTF-16 content
func decodeUTF16(t string, data []byte) string {
var dec transform.Transformer
if t == "be" {
dec = unicode.UTF16(unicode.BigEndian, unicode.ExpectBOM).NewDecoder()
} else if t == "le" {
dec = unicode.UTF16(unicode.LittleEndian, unicode.ExpectBOM).NewDecoder()
}
return line[start : start+end]

r := transform.NewReader(bytes.NewReader(data), dec)
b, _ := io.ReadAll(r)
return string(b)
}

// remove non-utf8 bytes
func clean(s string) string {
r := strings.NewReplacer(
"\x00", "",
"\xFF", "",
"\xFE", "",
)

return r.Replace(s)
}
Loading