This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAMV.go
267 lines (208 loc) · 5.44 KB
/
AMV.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package arn
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"github.com/aerogo/nano"
"github.com/animenotifier/arn/video"
)
// AMV is an anime music video.
type AMV struct {
File string `json:"file" editable:"true" type:"upload" filetype:"video" endpoint:"/api/upload/amv/:id/file"`
Title AMVTitle `json:"title" editable:"true"`
MainAnimeID string `json:"mainAnimeId" editable:"true"`
ExtraAnimeIDs []string `json:"extraAnimeIds" editable:"true"`
VideoEditorIDs []string `json:"videoEditorIds" editable:"true"`
Links []Link `json:"links" editable:"true"`
Tags []string `json:"tags" editable:"true"`
Info video.Info `json:"info"`
hasID
hasPosts
hasCreator
hasEditor
hasLikes
hasDraft
}
// Link returns the permalink for the AMV.
func (amv *AMV) Link() string {
return "/amv/" + amv.ID
}
// TitleByUser returns the preferred title for the given user.
func (amv *AMV) TitleByUser(user *User) string {
return amv.Title.ByUser(user)
}
// SetVideoBytes sets the bytes for the video file.
func (amv *AMV) SetVideoBytes(data []byte) error {
fileName := amv.ID + ".webm"
filePath := path.Join(Root, "videos", "amvs", fileName)
err := ioutil.WriteFile(filePath, data, 0644)
if err != nil {
return err
}
// Run mkclean
optimizedFile := filePath + ".optimized"
cmd := exec.Command(
"mkclean",
"--doctype", "4",
"--keep-cues",
"--optimize",
filePath,
optimizedFile,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
// Now delete the original file and replace it with the optimized file
err = os.Remove(filePath)
if err != nil {
return err
}
err = os.Rename(optimizedFile, filePath)
if err != nil {
return err
}
// Refresh video file info
amv.File = fileName
return amv.RefreshInfo()
}
// RefreshInfo refreshes the information about the video file.
func (amv *AMV) RefreshInfo() error {
if amv.File == "" {
return fmt.Errorf("Video file has not been uploaded yet for AMV %s", amv.ID)
}
info, err := video.GetInfo(path.Join(Root, "videos", "amvs", amv.File))
if err != nil {
return err
}
amv.Info = *info
return nil
}
// MainAnime returns main anime for the AMV, if available.
func (amv *AMV) MainAnime() *Anime {
mainAnime, _ := GetAnime(amv.MainAnimeID)
return mainAnime
}
// ExtraAnime returns main anime for the AMV, if available.
func (amv *AMV) ExtraAnime() []*Anime {
objects := DB.GetMany("Anime", amv.ExtraAnimeIDs)
animes := []*Anime{}
for _, obj := range objects {
if obj == nil {
continue
}
animes = append(animes, obj.(*Anime))
}
return animes
}
// VideoEditors returns a slice of all the users involved in creating the AMV.
func (amv *AMV) VideoEditors() []*User {
objects := DB.GetMany("User", amv.VideoEditorIDs)
editors := []*User{}
for _, obj := range objects {
if obj == nil {
continue
}
editors = append(editors, obj.(*User))
}
return editors
}
// Publish turns the draft into a published object.
func (amv *AMV) Publish() error {
// No title
if amv.Title.String() == "" {
return errors.New("AMV doesn't have a title")
}
// No anime found
if amv.MainAnimeID == "" && len(amv.ExtraAnimeIDs) == 0 {
return errors.New("Need to specify at least one anime")
}
// No file uploaded
if amv.File == "" {
return errors.New("You need to upload a WebM file for this AMV")
}
if _, err := os.Stat(path.Join(Root, "videos", "amvs", amv.File)); os.IsNotExist(err) {
return errors.New("You need to upload a WebM file for this AMV")
}
return publish(amv)
}
// Unpublish turns the object back into a draft.
func (amv *AMV) Unpublish() error {
return unpublish(amv)
}
// OnLike is called when the AMV receives a like.
func (amv *AMV) OnLike(likedBy *User) {
if likedBy.ID == amv.CreatedBy {
return
}
go func() {
amv.Creator().SendNotification(&PushNotification{
Title: likedBy.Nick + " liked your AMV " + amv.Title.ByUser(amv.Creator()),
Message: likedBy.Nick + " liked your AMV " + amv.Title.ByUser(amv.Creator()) + ".",
Icon: "https:" + likedBy.AvatarLink("large"),
Link: "https://notify.moe" + likedBy.Link(),
Type: NotificationTypeLike,
})
}()
}
// String implements the default string serialization.
func (amv *AMV) String() string {
return amv.Title.ByUser(nil)
}
// TypeName returns the type name.
func (amv *AMV) TypeName() string {
return "AMV"
}
// Self returns the object itself.
func (amv *AMV) Self() Loggable {
return amv
}
// GetAMV returns the AMV with the given ID.
func GetAMV(id string) (*AMV, error) {
obj, err := DB.Get("AMV", id)
if err != nil {
return nil, err
}
return obj.(*AMV), nil
}
// StreamAMVs returns a stream of all AMVs.
func StreamAMVs() <-chan *AMV {
channel := make(chan *AMV, nano.ChannelBufferSize)
go func() {
for obj := range DB.All("AMV") {
channel <- obj.(*AMV)
}
close(channel)
}()
return channel
}
// AllAMVs returns a slice of all AMVs.
func AllAMVs() []*AMV {
all := make([]*AMV, 0, DB.Collection("AMV").Count())
stream := StreamAMVs()
for obj := range stream {
all = append(all, obj)
}
return all
}
// FilterAMVs filters all AMVs by a custom function.
func FilterAMVs(filter func(*AMV) bool) []*AMV {
var filtered []*AMV
for obj := range DB.All("AMV") {
realObject := obj.(*AMV)
if filter(realObject) {
filtered = append(filtered, realObject)
}
}
return filtered
}