Skip to content

Commit

Permalink
use directory.SubDir for path join and existence check
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaslovsky committed Jan 29, 2023
1 parent 430fbc2 commit 40b1b1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
22 changes: 15 additions & 7 deletions pkg/thread/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,33 @@ type Directory struct {
// NewDirectory constructs a new Directory by formatting the name and joining with topLevelDir
func NewDirectory(topLevelDir string, name string) *Directory {
return &Directory{
path: filepath.Join(topLevelDir, strings.Replace(name, " ", "_", -1)),
path: filepath.Clean(filepath.Join(topLevelDir, strings.Replace(name, " ", "_", -1))),
}
}

// Create creates a Directory
func (d *Directory) Create() error {
return os.MkdirAll(d.path, 0o750)
}

// Exists evaluates if a Directory exists
func (d *Directory) Exists(subpaths ...string) bool {
_, err := os.Stat(d.Join(subpaths...))
func (d *Directory) Exists() bool {
_, err := os.Stat(d.path)
return !os.IsNotExist(err)
}

// Create creates a Directory
func (d *Directory) Create() error {
return os.MkdirAll(filepath.Clean(d.path), 0o750)
// SubDir constructs a file path by joining any provided subpath strings to the Directory path and
// returns the path and a bool indicating if the constructed path exists
func (d *Directory) SubDir(subpaths ...string) (string, bool) {
path := d.Join(subpaths...)
_, err := os.Stat(path)
return path, !os.IsNotExist(err)
}

// Join constructs a string representing a path to a subdirectory or file of a Directory
func (d *Directory) Join(subpaths ...string) string {
parts := append([]string{d.path}, subpaths...)
return filepath.Join(parts...)
return filepath.Clean(filepath.Join(parts...))
}

func (d *Directory) String() string {
Expand Down
21 changes: 9 additions & 12 deletions pkg/thread/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func FromJSON(appDir string, threadName string) (*Thread, error) {
return nil, fmt.Errorf("%s not found", dir)
}

jsonFile := filepath.Clean(dir.Join(fileNameJSON))
b, err := os.ReadFile(jsonFile)
b, err := os.ReadFile(dir.Join(fileNameJSON))
if err != nil {
return nil, err
}
Expand All @@ -53,8 +52,7 @@ func (th *Thread) ToJSON() error {
return err
}

jsonFile := filepath.Clean(th.Dir.Join(fileNameJSON))
return os.WriteFile(jsonFile, b, 0o600)
return os.WriteFile(th.Dir.Join(fileNameJSON), b, 0o600)
}

// ToHTML generates and saves an HTML file from a thread using default or provided template and CSS files
Expand All @@ -69,10 +67,9 @@ func (th *Thread) ToHTML(templateFile string, cssFile string) error {
return fmt.Errorf("failed to parse template: %w", tErr)
}

htmlFile := filepath.Clean(th.Dir.Join(fileNameHTML))
f, fErr := os.Create(htmlFile)
f, fErr := os.Create(th.Dir.Join(fileNameHTML))
if fErr != nil {
return fmt.Errorf("failed to open file %s: %w", htmlFile, fErr)
return fmt.Errorf("failed to open HTML file: %w", fErr)
}
defer func() {
_ = f.Close()
Expand All @@ -95,7 +92,7 @@ func (th *Thread) DownloadAttachments() error {

for _, tweet := range th.Tweets {
for _, attachment := range tweet.Attachments {
err := attachment.Download(filepath.Clean(attachmentDir.Join(attachment.Name(tweet.ID))))
err := attachment.Download(attachmentDir.Join(attachment.Name(tweet.ID)))
if err != nil {
return err
}
Expand All @@ -111,8 +108,8 @@ func loadHTMLTemplateFile(threadDir *Directory, templateFile string) (string, er
}

// Try to load default template from file
if threadDir.Exists("..", fileNameTemplateDefault) {
return readFile(threadDir.Join("..", fileNameTemplateDefault))
if defaultFile, exists := threadDir.SubDir("..", fileNameTemplateDefault); exists {
return readFile(defaultFile)
}

return "", nil
Expand All @@ -124,8 +121,8 @@ func getCSSFile(threadDir *Directory, cssFile string) string {
}

// Try to load default CSS file
if threadDir.Exists("..", fileNameCSSDefault) {
return threadDir.Join("..", fileNameCSSDefault)
if defaultFile, exists := threadDir.SubDir("..", fileNameCSSDefault); exists {
return defaultFile
}

return ""
Expand Down
2 changes: 1 addition & 1 deletion pkg/thread/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewTemplateThread(th *Thread) TemplateThread {
attachmentFile := attachment.Name(tweet.ID)

// Skip attachment if not downloaded
if !attachmentDir.Exists(attachmentFile) {
if _, exists := attachmentDir.SubDir(attachmentFile); !exists {
continue
}

Expand Down

0 comments on commit 40b1b1a

Please sign in to comment.