refactor(media): centralize temp media dir path
This commit is contained in:
parent
555af137b4
commit
b9aaad95cd
5 changed files with 19 additions and 6 deletions
|
|
@ -618,7 +618,7 @@ func (c *FeishuChannel) downloadResource(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to the shared picoclaw_media directory using a unique name to avoid collisions.
|
// Write to the shared picoclaw_media directory using a unique name to avoid collisions.
|
||||||
mediaDir := filepath.Join(os.TempDir(), "picoclaw_media")
|
mediaDir := media.TempDir()
|
||||||
if mkdirErr := os.MkdirAll(mediaDir, 0o700); mkdirErr != nil {
|
if mkdirErr := os.MkdirAll(mediaDir, 0o700); mkdirErr != nil {
|
||||||
logger.ErrorCF("feishu", "Failed to create media directory", map[string]any{
|
logger.ErrorCF("feishu", "Failed to create media directory", map[string]any{
|
||||||
"error": mkdirErr.Error(),
|
"error": mkdirErr.Error(),
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,6 @@ const (
|
||||||
roomKindCacheTTL = 5 * time.Minute
|
roomKindCacheTTL = 5 * time.Minute
|
||||||
roomKindCacheCleanupPeriod = 1 * time.Minute
|
roomKindCacheCleanupPeriod = 1 * time.Minute
|
||||||
roomKindCacheMaxEntries = 2048
|
roomKindCacheMaxEntries = 2048
|
||||||
|
|
||||||
matrixMediaTempDirName = "picoclaw_media"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var matrixMentionHrefRegexp = regexp.MustCompile(`(?i)<a[^>]+href=["']([^"']+)["']`)
|
var matrixMentionHrefRegexp = regexp.MustCompile(`(?i)<a[^>]+href=["']([^"']+)["']`)
|
||||||
|
|
@ -1105,7 +1103,7 @@ func (c *MatrixChannel) stripSelfMention(text string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func matrixMediaTempDir() (string, error) {
|
func matrixMediaTempDir() (string, error) {
|
||||||
mediaDir := filepath.Join(os.TempDir(), matrixMediaTempDirName)
|
mediaDir := media.TempDir()
|
||||||
if err := os.MkdirAll(mediaDir, 0o700); err != nil {
|
if err := os.MkdirAll(mediaDir, 0o700); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/config"
|
"github.com/sipeed/picoclaw/pkg/config"
|
||||||
|
"github.com/sipeed/picoclaw/pkg/media"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMatrixLocalpartMentionRegexp(t *testing.T) {
|
func TestMatrixLocalpartMentionRegexp(t *testing.T) {
|
||||||
|
|
@ -165,7 +166,7 @@ func TestMatrixMediaTempDir(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("matrixMediaTempDir failed: %v", err)
|
t.Fatalf("matrixMediaTempDir failed: %v", err)
|
||||||
}
|
}
|
||||||
if filepath.Base(dir) != matrixMediaTempDirName {
|
if filepath.Base(dir) != media.TempDirName {
|
||||||
t.Fatalf("unexpected media dir base: %q", filepath.Base(dir))
|
t.Fatalf("unexpected media dir base: %q", filepath.Base(dir))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
13
pkg/media/tempdir.go
Normal file
13
pkg/media/tempdir.go
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
package media
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
const TempDirName = "picoclaw_media"
|
||||||
|
|
||||||
|
// TempDir returns the shared temporary directory used for downloaded media.
|
||||||
|
func TempDir() string {
|
||||||
|
return filepath.Join(os.TempDir(), TempDirName)
|
||||||
|
}
|
||||||
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/logger"
|
"github.com/sipeed/picoclaw/pkg/logger"
|
||||||
|
"github.com/sipeed/picoclaw/pkg/media"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsAudioFile checks if a file is an audio file based on its filename extension and content type.
|
// IsAudioFile checks if a file is an audio file based on its filename extension and content type.
|
||||||
|
|
@ -67,7 +68,7 @@ func DownloadFile(urlStr, filename string, opts DownloadOptions) string {
|
||||||
opts.LoggerPrefix = "utils"
|
opts.LoggerPrefix = "utils"
|
||||||
}
|
}
|
||||||
|
|
||||||
mediaDir := filepath.Join(os.TempDir(), "picoclaw_media")
|
mediaDir := media.TempDir()
|
||||||
if err := os.MkdirAll(mediaDir, 0o700); err != nil {
|
if err := os.MkdirAll(mediaDir, 0o700); err != nil {
|
||||||
logger.ErrorCF(opts.LoggerPrefix, "Failed to create media directory", map[string]any{
|
logger.ErrorCF(opts.LoggerPrefix, "Failed to create media directory", map[string]any{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue