Skip to content

Commit 9694af1

Browse files
author
dularion
committed
add handling for subtitles for MOVIES for bulkCreation
1 parent ee0dee0 commit 9694af1

File tree

4 files changed

+108
-19
lines changed

4 files changed

+108
-19
lines changed

grails-app/assets/javascripts/streama/controllers/createFromFileCtrl.modal.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ angular.module('streama')
55

66
function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadService, dialogOptions, modalService, $state) {
77
var vm = this;
8+
var STATUS_NO_MATCH = 0;
9+
var STATUS_MATCH_FOUND = 1;
10+
var STATUS_EXISTING = 2;
11+
var STATUS_CREATED = 3;
12+
var STATUS_LIMIT_REACHED = 4;
13+
var STATUS_EXISTING_FOR_SUBTITLE = 5;
14+
var STATUS_SUBTITLE_MATCH = 6;
15+
var STATUS_SUBTITLE_ADDED = 7;
16+
817
vm.loading = false;
918
vm.localFilesEnabled = false;
1019
vm.localFiles = [];
@@ -30,6 +39,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
3039
vm.openMediaDetail = openMediaDetail;
3140
vm.openAdminForm = openAdminForm;
3241
vm.isSelected = isSelected;
42+
vm.hasStatus = hasStatus;
3343

3444

3545
init();
@@ -109,7 +119,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
109119
vm.isMatcherLoading = false;
110120
vm.matchResult = data;
111121
//console.log(data);
112-
deselectByStatus(2);
122+
deselectByStatus(STATUS_EXISTING);
113123
});
114124
}
115125

@@ -162,14 +172,16 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
162172

163173

164174
function addAllMatches() {
165-
var allFoundMatches = _.filter(vm.matchResult, {status: 1});
175+
var allFoundMatches = _.filter(vm.matchResult, function (match) {
176+
return (match.status === STATUS_MATCH_FOUND || match.status === STATUS_EXISTING_FOR_SUBTITLE || match.status === STATUS_SUBTITLE_MATCH)
177+
});
166178
if(allFoundMatches.length == 0){
167179
alertify.success('Nothing to add.');
168180
}
169181

170182
apiService.file.bulkAddMediaFromFile(allFoundMatches).then(function (response) {
171183
var data = response.data;
172-
if(_.some(data, {status: 4})){
184+
if(_.some(data, {status: STATUS_LIMIT_REACHED})){
173185
alertify.log("not all files were added unfortunately. This is due to TheMovieDB API LIMIT constraints. Just try again in a couple of seconds :). ")
174186
}else{
175187
alertify.success("All matches have been added to the database and files connected");
@@ -183,7 +195,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
183195
var fileMatch = _.find(vm.matchResult, {"file": file.path});
184196
apiService.file.bulkAddMediaFromFile([fileMatch]).then(function (response) {
185197
var result = response.data;
186-
if(_.some(result, {status: 4})){
198+
if(_.some(result, {status: STATUS_LIMIT_REACHED})){
187199
alertify.log("not all files were added unfortunately. This is due to TheMovieDB API LIMIT constraints. Just try again in a couple of seconds :) ")
188200
}else{
189201
alertify.success(fileMatch.title || fileMatch.episodeName + " has been added");
@@ -197,7 +209,7 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
197209
return _.find(result, {file: match.file}) || match;
198210
});
199211

200-
deselectByStatus(3);
212+
deselectByStatus(STATUS_CREATED);
201213
}
202214

203215

@@ -208,4 +220,12 @@ function modalCreateFromFileCtrl($scope, $uibModalInstance, apiService, uploadSe
208220
});
209221
}
210222

223+
function hasStatus(file, status) {
224+
var matchForPath = getMatchForPath(file.path);
225+
if(!matchForPath){
226+
return false;
227+
}
228+
return (matchForPath.status === status);
229+
}
230+
211231
}

grails-app/assets/javascripts/streama/templates/modal--create-from-file-recursive-item.tpl.htm

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<span ng-if="vm.getMatchForPath(file.path)">
1111
{{vm.getMatchForPath(file.path).message}}
1212
</span>
13-
<button class="btn btn-xs btn-success button-icon" ng-if="vm.getMatchForPath(file.path).status == 1"
13+
<button class="btn btn-xs btn-success button-icon" ng-if="vm.hasStatus(file, 1)"
1414
ng-click="vm.addSelectedFile()">Add this file
1515
</button>
1616
</div>
@@ -31,30 +31,46 @@
3131
<i class="ion-document"></i> {{ file.name }}
3232
</div>
3333
<div class="col-md-3" style="padding-right: 0;">
34-
<i class="ion-load-c spin column-loading" ng-show="vm.isMatcherLoading && vm.isSelected(file) && (!vm.getMatchForPath(file.path) || vm.getMatchForPath(file.path).status == 1)"></i>
35-
<span ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 1">
36-
<a ng-click="vm.openMediaDetail(vm.getMatchForPath(file.path))"><i class="ion-ios-eye"></i> Match: {{vm.getMatchDisplay(file)}}</a>
34+
<i class="ion-load-c spin column-loading"
35+
ng-show="vm.isMatcherLoading && vm.isSelected(file) && (!vm.getMatchForPath(file.path) || vm.hasStatus(file, 1))"></i>
36+
<span ng-if="vm.getMatchForPath(file.path) && (vm.hasStatus(file, 1) || vm.hasStatus(file, 6))">
37+
<a ng-click="vm.openMediaDetail(vm.getMatchForPath(file.path))">
38+
<i class="ion-ios-eye"></i>
39+
{{vm.hasStatus(file, 6) ? 'Subtitle': ''}} Match: {{vm.getMatchDisplay(file)}}
40+
</a>
3741
</span>
38-
<span ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 2">
42+
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 2)">
3943
already added
4044
</span>
41-
<span ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 3">
45+
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 3)">
4246
created
4347
</span>
44-
<span ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 0">
48+
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 7)">
49+
subtitle added
50+
</span>
51+
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 5)">
52+
found Media for Subtitle:
53+
<a ng-click="vm.openAdminForm(vm.getMatchForPath(file.path))"
54+
class="text-success">{{vm.getMatchDisplay(file)}} <i class="ion-android-open"></i></a>
55+
56+
</span>
57+
<span ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 0)">
4558
{{vm.getMatchForPath(file.path).message}}
4659
</span>
47-
<span class="text-danger" ng-if="vm.getMatchForPath(file.path) && vm.getMatchForPath(file.path).status == 4">
60+
<span class="text-danger" ng-if="vm.getMatchForPath(file.path) && vm.hasStatus(file, 4)">
4861
TheMovieDB Error: &nbsp;<i class="ion-help-circled" title="{{vm.getMatchForPath(file.path).errorMessage}}"></i>
4962
</span>
5063

5164
<br>
5265

53-
<button class="btn btn-xs btn-success button-icon" ng-if="vm.getMatchForPath(file.path).status == 1"
66+
<button class="btn btn-xs btn-success button-icon" ng-if="vm.hasStatus(file, 1)"
5467
ng-click="vm.addSelectedFile(file)">Add this file
5568
</button>
56-
<a ng-if="vm.getMatchForPath(file.path).status == 2 || vm.getMatchForPath(file.path).status == 3"
69+
<button class="btn btn-xs btn-success button-icon" ng-if="vm.hasStatus(file, 5)"
70+
ng-click="vm.addSelectedFile(file)">Add subtitle file
71+
</button>
72+
<a ng-if="vm.hasStatus(file, 2) || vm.hasStatus(file, 3) || vm.hasStatus(file, 7)"
5773
ng-click="vm.openAdminForm(vm.getMatchForPath(file.path))"
58-
class="text-success">Match: {{vm.getMatchDisplay(file)}} <i class="ion-android-open"></i></a>
74+
class="text-success">Go to: {{vm.getMatchDisplay(file)}} <i class="ion-android-open"></i></a>
5975
</div>
6076
</div>

grails-app/services/streama/BulkCreateService.groovy

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class BulkCreateService {
1818
MATCH_FOUND: 1,
1919
EXISTING: 2,
2020
CREATED: 3,
21-
LIMIT_REACHED: 4
21+
LIMIT_REACHED: 4,
22+
EXISTING_FOR_SUBTITLE: 5,
23+
SUBTITLE_MATCH: 6,
24+
SUBTITLE_ADDED: 7
2225
]
2326
final static STREAMA_ROUTES = [
2427
movie: 'movie',
@@ -82,6 +85,7 @@ class BulkCreateService {
8285
def name = movieMatcher.group('Name').replaceAll(/[._]/, " ")
8386
def year = movieRegex.contains('<Year>') ? movieMatcher.group('Year') : null
8487
def type = "movie"
88+
Boolean isSubtitle = VideoHelper.isSubtitleFile(fileResult.file)
8589

8690
try {
8791
def json = theMovieDbService.searchForEntry(type, name, year)
@@ -94,7 +98,11 @@ class BulkCreateService {
9498

9599
Movie existingMovie = Movie.findByApiIdAndDeletedNotEqual(movieResult.id, true)
96100
if(existingMovie){
97-
fileResult.status = MATCHER_STATUS.EXISTING
101+
if(isSubtitle){
102+
fileResult.status = MATCHER_STATUS.EXISTING_FOR_SUBTITLE
103+
}else{
104+
fileResult.status = MATCHER_STATUS.EXISTING
105+
}
98106
fileResult.importedId = existingMovie.id
99107
fileResult.importedType = STREAMA_ROUTES[type]
100108
}
@@ -113,6 +121,9 @@ class BulkCreateService {
113121
fileResult.title = name
114122
}
115123
fileResult.status = fileResult.status ?: MATCHER_STATUS.MATCH_FOUND
124+
if(fileResult.status == MATCHER_STATUS.MATCH_FOUND && isSubtitle){
125+
fileResult.status = MATCHER_STATUS.SUBTITLE_MATCH
126+
}
116127
fileResult.message = 'match found'
117128
fileResult.type = type
118129
}
@@ -204,7 +215,10 @@ class BulkCreateService {
204215
@NotTransactional
205216
def bulkAddMediaFromFile(List<Map> fileMatchers){
206217
def result = []
207-
fileMatchers.each{ fileMatcher ->
218+
List<Map> videoFiles = fileMatchers.findAll{!VideoHelper.isSubtitleFile(it.file)}
219+
List<Map> subtitleFiles = fileMatchers.findAll{VideoHelper.isSubtitleFile(it.file)}
220+
221+
videoFiles.each{ fileMatcher ->
208222
String type = fileMatcher.type
209223
def entity
210224
if(fileMatcher.status == MATCHER_STATUS.EXISTING){
@@ -233,6 +247,29 @@ class BulkCreateService {
233247
result.add(fileMatcher)
234248
}
235249

250+
subtitleFiles.each{ fileMatcher ->
251+
String type = fileMatcher.type
252+
Video videoInstance
253+
254+
if(type == 'movie'){
255+
videoInstance = Movie.findByApiIdAndDeletedNotEqual(fileMatcher.apiId, true)
256+
}
257+
else if(type == 'episode'){
258+
videoInstance = Episode.findByApiIdAndDeletedNotEqual(fileMatcher.apiId, true)
259+
}
260+
261+
if(!videoInstance){
262+
log.error("no video found for subtitle")
263+
return
264+
}
265+
videoInstance.addLocalFile(fileMatcher.file)
266+
267+
fileMatcher.status = MATCHER_STATUS.SUBTITLE_ADDED
268+
fileMatcher.importedId = videoInstance instanceof Episode ? videoInstance.showId : videoInstance.id
269+
fileMatcher.importedType = STREAMA_ROUTES[type]
270+
result.add(fileMatcher)
271+
}
272+
236273
return result
237274
}
238275
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package streama
2+
3+
class VideoHelper {
4+
5+
static String getExtensionFromFilename(String filename){
6+
Integer extensionIndex = filename.lastIndexOf('.')
7+
String extension = filename[extensionIndex..-1]
8+
9+
return extension
10+
}
11+
12+
static Boolean isSubtitleFile(String filename){
13+
String extension = getExtensionFromFilename(filename)
14+
return (extension == '.srt' || extension == '.vtt')
15+
}
16+
}

0 commit comments

Comments
 (0)