Skip to content

Commit

Permalink
feat: ignore .files and add .calnotes and .caltrash to calibre option (
Browse files Browse the repository at this point in the history
  • Loading branch information
dubyte authored Jun 14, 2024
1 parent da71c71 commit ac66c6f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2024-06-14

### Changed

- when calibre option is passed now also excludes .calnotes .caltrash

### Added

- hide-dot-files can be passed to ignere .files

## [1.0.6] - 2024-03-12

### Security
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ go install github.com/dubyte/dir2opds@latest
```bash
Usage of dir2opds:
-calibre
Hide files stored by calibre
Hide files stored by calibre.
-debug
If it is set, it will log the requests
If it is set it will log the requests.
-dir string
A directory with books (default "./books")
A directory with books. (default "./books")
-hide-dot-files
Hide files that starts with dot.
-host string
The server will listen in this host (default "0.0.0.0")
The server will listen in this host. (default "0.0.0.0")
-port string
The server will listen in this port (default "8080")
The server will listen in this port. (default "8080")
```

## Tested on
Expand All @@ -58,9 +60,9 @@ Usage of dir2opds:
cd && mkdir dir2opds && cd dir2opds

# get the binary
wget https://github.com/dubyte/dir2opds/releases/download/v0.0.10/dir2opds_0.0.10_Linux_ARMv7.tar.gz
wget https://github.com/dubyte/dir2opds/releases/download/v1.10.0/dir2opds_1.10.0_linux_armv7.tar.gz

tar xvf dir2opds_0.0.10_Linux_ARMv7.tar.gz
tar xvf dir2opds_1.10.0_linux_armv7.tar.gz

sudo touch /etc/systemd/system/dir2opds.service

Expand Down
41 changes: 35 additions & 6 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ const (
pathTypeDirOfFiles
)

const (
ignoreFile = true
includeFile = false
currentDirectory = "."
parentDirectory = ".."
hiddenFilePrefix = "."
)

type OPDS struct {
TrustedRoot string
IsCalibreLibrary bool
HideCalibreFiles bool
HideDotFiles bool
}

type IsDirer interface {
Expand Down Expand Up @@ -122,11 +131,8 @@ func (s OPDS) makeFeed(fpath string, req *http.Request) atom.Feed {

dirEntries, _ := os.ReadDir(fpath)
for _, entry := range dirEntries {
// ignoring files created by calibre
if s.IsCalibreLibrary && (strings.Contains(entry.Name(), ".opf") ||
strings.Contains(entry.Name(), "cover.") ||
strings.Contains(entry.Name(), "metadata.db") ||
strings.Contains(entry.Name(), "metadata_db_prefs_backup.json")) {

if fileShouldBeIgnored(entry.Name(), s.HideCalibreFiles, s.HideDotFiles) {
continue
}

Expand All @@ -146,6 +152,29 @@ func (s OPDS) makeFeed(fpath string, req *http.Request) atom.Feed {
return feedBuilder.Build()
}

func fileShouldBeIgnored(filename string, hideCalibreFiles, hideDotFiles bool) bool {
// not ignore those directories
if filename == currentDirectory || filename == parentDirectory {
return includeFile
}

if hideDotFiles && strings.HasPrefix(filename, hiddenFilePrefix) {
return ignoreFile
}

if hideCalibreFiles &&
(strings.Contains(filename, ".opf") ||
strings.Contains(filename, "cover.") ||
strings.Contains(filename, "metadata.db") ||
strings.Contains(filename, "metadata_db_prefs_backup.json") ||
strings.Contains(filename, ".caltrash") ||
strings.Contains(filename, ".calnotes")) {
return ignoreFile
}

return false
}

func getRel(name string, pathType int) string {
if pathType == pathTypeDirOfFiles || pathType == pathTypeDirOfDirs {
return "subsection"
Expand Down
2 changes: 1 addition & 1 deletion internal/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestHandler(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// setup
s := service.OPDS{"testdata", true}
s := service.OPDS{"testdata", true, true}
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, tc.input, nil)
service.TimeNow = func() time.Time {
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import (
)

var (
port = flag.String("port", "8080", "The server will listen in this port")
host = flag.String("host", "0.0.0.0", "The server will listen in this host")
dirRoot = flag.String("dir", "./books", "A directory with books")
debug = flag.Bool("debug", false, "If it is set it will log the requests")
calibre = flag.Bool("calibre", false, "Hide files stored by calibre")
port = flag.String("port", "8080", "The server will listen in this port.")
host = flag.String("host", "0.0.0.0", "The server will listen in this host.")
dirRoot = flag.String("dir", "./books", "A directory with books.")
debug = flag.Bool("debug", false, "If it is set it will log the requests.")
calibre = flag.Bool("calibre", false, "Hide files stored by calibre.")
hideDotFiles = flag.Bool("hide-dot-files", false, "Hide files that starts with dot.")
)

func main() {
Expand All @@ -56,7 +57,7 @@ func main() {

log.Printf("%q will be used as your trusted root", *dirRoot)

s := service.OPDS{TrustedRoot: *dirRoot, IsCalibreLibrary: *calibre}
s := service.OPDS{TrustedRoot: *dirRoot, HideCalibreFiles: *calibre, HideDotFiles: *hideDotFiles}

http.HandleFunc("/", errorHandler(s.Handler))

Expand Down

0 comments on commit ac66c6f

Please sign in to comment.