-
Notifications
You must be signed in to change notification settings - Fork 983
/
VideoService.groovy
199 lines (161 loc) · 5.33 KB
/
VideoService.groovy
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
package streama
import grails.transaction.Transactional
import grails.web.servlet.mvc.GrailsParameterMap
import org.grails.web.util.WebUtils
import java.nio.file.Files
import java.nio.file.Paths
import static org.springframework.http.HttpStatus.NOT_ACCEPTABLE
import static org.springframework.http.HttpStatus.NOT_ACCEPTABLE
@Transactional
class VideoService {
def fileService
def uploadService
def deleteVideoAndAssociations(Video video) {
video.setDeleted(true)
ViewingStatus.findAllByVideo(video)*.delete(flush: true)
video.files?.each{ fileService.fullyRemoveFile(it) }
video.save failOnError: true, flush: true
}
public static List<ViewingStatus> listContinueWatching(User currentUser, Profile profile) {
List<ViewingStatus> continueWatching = ViewingStatus.withCriteria {
eq("user", currentUser)
eq("profile", profile)
video {
isNotEmpty("files")
ne("deleted", true)
}
// eq("completed", false)
order("lastUpdated", "desc")
}
return reduceContinueWatchingEps(continueWatching)
}
private static List<ViewingStatus> reduceContinueWatchingEps(List<ViewingStatus> continueWatching) {
def result = []
continueWatching.each { continueWatchingItem ->
if (continueWatchingItem.video instanceof Episode) {
def previousShowEntry = result.find { it.video instanceof Episode && it.video.show?.id == continueWatchingItem.video.show?.id }
if (!previousShowEntry) {
result.add(continueWatchingItem)
}
} else {
result.add(continueWatchingItem)
}
}
return result
}
@Transactional
def addLocalFile(Video videoInstance, params){
def result = [:]
// The local path is configured?
if (!uploadService.localPath) {
result.message = "The Local Video Files setting is not configured."
result.error = true
result.statusCode = NOT_ACCEPTABLE.value
return result
}
// The local path is configured?
if (!params.localFile) {
result.message = "No LocalFile has been submitted for this request."
result.error = true
result.statusCode = NOT_ACCEPTABLE.value
return result
}
// Check that the given file path is contained in the local files directory
def localPath = Paths.get(uploadService.localPath)
def givenPath = Paths.get(params.localFile).toAbsolutePath()
if (!givenPath.startsWith(localPath)) {
result.message = "The video file must be contained in the Local Video Files setting."
result.error = true
result.statusCode = NOT_ACCEPTABLE.value
return result
}
// Create the file in database
File file = File.findOrCreateByLocalFile(params.localFile)
file.localFile = params.localFile
file.originalFilename = givenPath.getFileName().toString()
file.contentType = Files.probeContentType(givenPath)
file.size = Files.size(givenPath)
def extensionIndex = params.localFile.lastIndexOf('.')
file.extension = params.localFile[extensionIndex..-1];
file.save(failOnError: true, flush: true)
videoInstance.addToFiles(file)
videoInstance.save(failOnError: true, flush: true)
return file
}
def listMovies(GrailsParameterMap params, Map options){
Profile currentProfile = User.getProfileFromRequest()
Integer max = params.int('max', 50)
Integer offset = params.int('offset', 0)
String sort = params.sort
String order = params.order
Long genreId = params.long('genreId')
List<Long> genreList = params.list('genre')*.toLong() ?: []
if(currentProfile?.isChild){
genreList += Genre.findAllByNameInList(['Kids', 'Family'])*.id
}
def movieQuery = Movie.where {
deleted != true
if(!options.includeEmpty){
isNotEmpty("files")
}
if(params.title){
title =~ "%${params.title}%"
}
if(genreId){
genre{
id == genreId
}
}
if(genreList){
genre{
id in genreList
}
}
}
def movies = movieQuery.list(max: max, offset: offset, sort: sort, order: order)
def totalMovieCount = movieQuery.count()
def result = [total: totalMovieCount, list: movies]
return result
}
def listShows(GrailsParameterMap params, Map options){
Profile currentProfile = User.getProfileFromRequest()
def max = params.int('max', 50)
def offset = params.int('offset', 0)
def sort = params.sort
def order = params.order
def genreId = params.long('genreId')
def genreList = params.list('genre')*.toLong() ?: []
if(currentProfile?.isChild){
genreList += Genre.findAllByNameInList(['Kids', 'Family'])*.id
}
def tvShowQuery = TvShow.where{
def tv1 = TvShow
deleted != true
if(!options.includeEmpty) {
exists Episode.where {
def ep = Episode
def tv2 = show
tv1.id == tv2.id
isNotEmpty("files")
}.id()
}
if(params.name){
name =~ "%${params.name}%"
}
if(genreId){
genre{
id == genreId
}
}
if(genreList){
genre{
id in genreList
}
}
}
def tvShows = tvShowQuery.list(max: max, offset: offset, sort: sort, order: order)
def totalTvShowsCount = tvShowQuery.count()
def result = [total: totalTvShowsCount, list: tvShows]
return result
}
}