aboutsummaryrefslogtreecommitdiff
path: root/utils/storage/mail.go
blob: 8f60f4f8db24dbb020586b8ff31de9dd3bade00f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package storage

import (
	"os"
	"path/filepath"
)

func MailFolderPath(mailboxAddress string, folderSlug string) string {
	return filepath.Join(DataDirectory, MailDirectory, mailboxAddress, folderSlug)
}

func MailFilePath(mailboxAddress string, folderSlug string, filename string) string {
	return filepath.Join(MailFolderPath(mailboxAddress, folderSlug), filename+EmlExtension)
}

func WriteMailFile(mailboxAddress string, folderSlug string, filename string, content []byte) error {
	directoryPath := MailFolderPath(mailboxAddress, folderSlug)

	if mkdirError := os.MkdirAll(directoryPath, 0750); mkdirError != nil {
		return mkdirError
	}

	filePath := MailFilePath(mailboxAddress, folderSlug, filename)
	return os.WriteFile(filePath, content, 0640)
}

func ReadMailFile(mailboxAddress string, folderSlug string, filename string) ([]byte, error) {
	filePath := MailFilePath(mailboxAddress, folderSlug, filename)
	return os.ReadFile(filePath)
}

func MoveMailFile(mailboxAddress string, sourceFolderSlug string, targetFolderSlug string, filename string) error {
	targetDirectory := MailFolderPath(mailboxAddress, targetFolderSlug)

	if mkdirError := os.MkdirAll(targetDirectory, 0750); mkdirError != nil {
		return mkdirError
	}

	sourcePath := MailFilePath(mailboxAddress, sourceFolderSlug, filename)
	targetPath := MailFilePath(mailboxAddress, targetFolderSlug, filename)

	return os.Rename(sourcePath, targetPath)
}

func DeleteMailFile(mailboxAddress string, folderSlug string, filename string) error {
	filePath := MailFilePath(mailboxAddress, folderSlug, filename)
	return os.Remove(filePath)
}