This repository was archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia.go
116 lines (102 loc) · 2.84 KB
/
media.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2022 The Takeout Authors.
//
// This file is part of Takeout.
//
// Takeout is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// Takeout is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
// more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Takeout. If not, see <https://www.gnu.org/licenses/>.
package server
import (
"fmt"
"github.com/defsub/takeout/auth"
"github.com/defsub/takeout/config"
"github.com/defsub/takeout/lib/log"
"github.com/defsub/takeout/music"
"github.com/defsub/takeout/podcast"
"github.com/defsub/takeout/video"
)
type Media struct {
config *config.Config
music *music.Music
video *video.Video
podcast *podcast.Podcast
}
func (m Media) Config() *config.Config {
return m.config
}
func (m Media) Music() *music.Music {
return m.music
}
func (m Media) Podcast() *podcast.Podcast {
return m.podcast
}
func (m Media) Video() *video.Video {
return m.video
}
func mediaConfigFor(root *config.Config, user *auth.User) (string, *config.Config, error) {
// only supports one media collection right now
mediaName := user.FirstMedia()
if mediaName == "" {
return "", nil, ErrNoMedia
}
config, err := mediaConfig(root, mediaName)
return mediaName, config, err
}
func mediaConfig(root *config.Config, mediaName string) (*config.Config, error) {
path := fmt.Sprintf("%s/%s", root.Server.MediaDir, mediaName)
// load relative media configuration
userConfig, err := config.LoadConfig(path)
if err != nil {
return nil, err
}
return userConfig, nil
}
var mediaMap map[string]*Media = make(map[string]*Media)
func makeMedia(name string, config *config.Config) *Media {
media, ok := mediaMap[name]
if !ok {
var err error
media = &Media{}
media.music, err = media.makeMusic(config)
log.CheckError(err)
media.video, err = media.makeVideo(config)
log.CheckError(err)
media.podcast, err = media.makePodcast(config)
log.CheckError(err)
mediaMap[name] = media
}
return media
}
func (Media) makeMusic(config *config.Config) (*music.Music, error) {
m := music.NewMusic(config)
err := m.Open()
if err != nil {
return nil, err
}
return m, nil
}
func (Media) makeVideo(config *config.Config) (*video.Video, error) {
v := video.NewVideo(config)
err := v.Open()
if err != nil {
return nil, err
}
return v, nil
}
func (Media) makePodcast(config *config.Config) (*podcast.Podcast, error) {
p := podcast.NewPodcast(config)
err := p.Open()
if err != nil {
return nil, err
}
return p, nil
}