- Updated environment configuration to unify frontend for mail and drive under a single service. - Revised README to reflect changes in frontend setup and routing for the unified application. - Introduced new API documentation endpoints for better accessibility of API specifications. - Enhanced drive and mail services with improved handling of file uploads and metadata enrichment. - Implemented new API token management features, including creation, listing, and revocation of tokens. - Added tests for new functionalities in drive and mail services to ensure reliability and correctness.
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package mail
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// DriveUploader copies mail attachment bytes into a user's Nextcloud drive.
|
|
type DriveUploader interface {
|
|
EnsureNextcloudFolder(ctx context.Context, email, sub, displayName, folderPath string) error
|
|
UploadFile(ctx context.Context, email, sub, displayName, destPath string, body io.Reader, contentType string, size int64) error
|
|
FileExists(ctx context.Context, email, sub, displayName, filePath string) (bool, error)
|
|
NotifyFileChanged(platformUserID, filePath string)
|
|
}
|
|
|
|
var ErrDriveUnavailable = errors.New("drive unavailable")
|
|
|
|
func normalizeDriveFolder(folderPath string) string {
|
|
p := strings.TrimSpace(folderPath)
|
|
if p == "" {
|
|
return "/"
|
|
}
|
|
if !strings.HasPrefix(p, "/") {
|
|
p = "/" + p
|
|
}
|
|
p = path.Clean(p)
|
|
if p == "." {
|
|
return "/"
|
|
}
|
|
return p
|
|
}
|
|
|
|
func uniqueDriveFilePath(ctx context.Context, uploader DriveUploader, email, sub, displayName, folderPath, filename string) (string, error) {
|
|
base := strings.TrimSpace(filename)
|
|
if base == "" {
|
|
base = "piece-jointe"
|
|
}
|
|
ext := path.Ext(base)
|
|
stem := strings.TrimSuffix(base, ext)
|
|
if stem == "" {
|
|
stem = base
|
|
ext = ""
|
|
}
|
|
|
|
candidate := path.Join(folderPath, base)
|
|
for i := 0; i < 100; i++ {
|
|
exists, err := uploader.FileExists(ctx, email, sub, displayName, candidate)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !exists {
|
|
return candidate, nil
|
|
}
|
|
if i == 0 {
|
|
candidate = path.Join(folderPath, stem+" (1)"+ext)
|
|
continue
|
|
}
|
|
candidate = path.Join(folderPath, stem+" ("+strconv.Itoa(i+1)+")"+ext)
|
|
}
|
|
return "", errors.New("could not allocate unique drive path")
|
|
}
|