Skip to content

Commit

Permalink
When attaching files to a post tagged with a project, copy the attach…
Browse files Browse the repository at this point in the history
…ed files to the project directory too
  • Loading branch information
thijzert committed Feb 20, 2023
1 parent 37c10cb commit e2ffdd5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 15 additions & 1 deletion bin/journal-server/journal-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,13 @@ func readAttachmentHashes(r *http.Request) []string {
}

func saveJournalEntry(timestamp time.Time, contents string, project string, attachmentIDs []string, starred bool) error {
project_attachments_dir := ""
var nonFatalError error
if project != "" && *projects_dir != "" {
prf := path.Join(*projects_dir, strings.Replace(strings.Replace(project, "/", "", -1), "\\", "", -1))
proj := strings.Replace(strings.Replace(project, "/", "", -1), "\\", "", -1)
prf := path.Join(*projects_dir, proj)
if f, err := os.OpenFile(prf, os.O_APPEND|os.O_WRONLY, 0600); err == nil {
project_attachments_dir = path.Join(*projects_dir, stripProjectSuffix(proj))
fmt.Fprintf(f, "\n=== %s ===\n%s\n", timestamp.Format("2006-01-02"), contents)
f.Close()
}
Expand All @@ -353,6 +356,17 @@ func saveJournalEntry(timestamp time.Time, contents string, project string, atta
}
f.Write(entry.Buf)
f.Close()

if project_attachments_dir != "" {
os.MkdirAll(project_attachments_dir, 0755)
f, err := os.Create(path.Join(project_attachments_dir, att_hash))
if err != nil {
nonFatalError = err
continue
}
f.Write(entry.Buf)
f.Close()
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion bin/journal-server/plumbing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ var daily *template.Template
var bwvlist *template.Template
var tie *template.Template

func formatProjectName(name string) string {
func stripProjectSuffix(name string) string {
if len(name) > 4 && name[len(name)-4:] == ".txt" {
name = name[:len(name)-5]
} else if len(name) > 5 && name[len(name)-5:] == ".wiki" {
name = name[:len(name)-5]
}
return name
}

func formatProjectName(name string) string {
name = stripProjectSuffix(name)
name = strings.Replace(name, "_", " ", -1)

return name
Expand Down

0 comments on commit e2ffdd5

Please sign in to comment.