Skip to content

Commit a91843f

Browse files
Enable videos for uploading additional files
1 parent f7e1156 commit a91843f

File tree

5 files changed

+100
-56
lines changed

5 files changed

+100
-56
lines changed

cds/modules/deposit/api.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,7 @@ def _rename_subtitles(self):
860860
subtitle_obj_key = "{}_{}.vtt".format(
861861
self["report_number"][0], match.group("iso_lang")
862862
)
863-
obj = ObjectVersion.create(
864-
bucket=subtitle_obj.bucket,
865-
key=subtitle_obj_key,
866-
_file_id=subtitle_obj.file_id,
867-
)
868-
# copy tags to the newly created object version
869-
for tag in subtitle_obj.tags:
870-
tag.object_version = obj
871-
subtitle_obj.remove()
863+
subtitle_obj.key = subtitle_obj_key
872864

873865
def _rename_master_file(self, master_file):
874866
"""Rename master file."""

cds/modules/deposit/ext.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"""CDSDeposit app for Webhook receivers."""
2626

2727
import re
28+
import mimetypes
2829

2930
from invenio_base.signals import app_loaded
3031
from invenio_db import db
@@ -45,38 +46,33 @@
4546

4647
def _create_tags(obj):
4748
"""Create additional tags for file."""
48-
# Subtitle file
49-
pattern = re.compile(".*_([a-zA-Z]{2})\.vtt$")
49+
pattern_subtitle = re.compile(r".*_([a-zA-Z]{2})\.vtt$")
50+
pattern_poster = re.compile(r"^poster\.(jpg|png)$")
51+
52+
# Get the media_type and content_type(file ext)
53+
file_name = obj.key
54+
mimetypes.add_type("subtitle/vtt", ".vtt")
55+
media_type = mimetypes.guess_type(file_name)[0].split("/")[0]
56+
file_ext = file_name.rsplit(".", 1)[-1].lower()
57+
5058
with db.session.begin_nested():
51-
# language tag
52-
found = pattern.findall(obj.key)
53-
if len(found) == 1:
54-
lang = found[0]
55-
ObjectVersionTag.create_or_update(obj, "language", lang)
56-
else:
57-
# clean to be sure there is no some previous value
58-
ObjectVersionTag.delete(obj, "language")
59-
# other tags
60-
ObjectVersionTag.create_or_update(obj, "content_type", "vtt")
61-
ObjectVersionTag.create_or_update(obj, "context_type", "subtitle")
62-
ObjectVersionTag.create_or_update(obj, "media_type", "subtitle")
63-
# refresh object
64-
db.session.add(obj)
59+
ObjectVersionTag.create_or_update(obj, "content_type", file_ext)
60+
ObjectVersionTag.create_or_update(obj, "media_type", media_type)
61+
if file_ext == "vtt":
62+
# language tag
63+
match = pattern_subtitle.search(file_name)
64+
if match:
65+
ObjectVersionTag.create_or_update(obj, "language", match.group(1))
66+
else:
67+
ObjectVersionTag.delete(obj, "language")
68+
# other tags
69+
ObjectVersionTag.create_or_update(obj, "content_type", "vtt")
70+
ObjectVersionTag.create_or_update(obj, "context_type", "subtitle")
71+
# poster tag
72+
elif pattern_poster.match(file_name):
73+
ObjectVersionTag.create_or_update(obj, "context_type", "poster")
6574

66-
# Poster frame
67-
pattern = re.compile("^poster\.(jpg|png)$")
68-
try:
69-
poster = pattern.findall(obj.key)
70-
if poster:
71-
ext = pattern.findall(poster.key)[0]
72-
# frame tags
73-
ObjectVersionTag.create_or_update(poster, "content_type", ext)
74-
ObjectVersionTag.create_or_update(poster, "context_type", "poster")
75-
ObjectVersionTag.create_or_update(poster, "media_type", "image")
76-
# refresh object
77-
db.session.add(poster)
78-
except IndexError:
79-
return
75+
db.session.add(obj)
8076

8177

8278
def create_tags_on_file_upload(sender, obj):

cds/modules/deposit/static/templates/cds_deposit/deposits.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ <h5 class="text-muted"><strong>Tips</strong></h5>
3939
<h3>Click here to select videos to upload</h3>
4040
</div>
4141
<p class="cds-deposit-box-upload-description">You can also <strong>Drag & Drop</strong> video files here</p>
42+
<p class="text-muted mt-20">supported files <mark>{{ $ctrl.videoExtensions }}</mark></p>
4243
</div>
4344
</div>
4445
</div>

cds/modules/deposit/static/templates/cds_deposit/types/video/uploader.html

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ <h5 class="text-muted"><strong>Tips and suggestions</strong></h5>
283283
</ul>
284284
</div>
285285
<!-- Error alert -->
286+
286287
<div class="cds-deposit-box" ng-if="!$ctrl.cdsDepositCtrl.isPublished()">
287288
<div
288289
ngf-drag-over-class="'cds-deposit-dragover'"
@@ -309,13 +310,36 @@ <h4>Upload complimentary files for this video</h4>
309310
<div class="text-muted">
310311
<h5 class="text-muted"><strong>Tips and suggestions</strong></h5>
311312
<ul>
312-
<li>To replace the video file, just upload another video.</li>
313+
<li>To replace the video file, just upload another video to here.</li>
313314
</ul>
315+
<div
316+
ngf-drag-over-class="{accept: 'cds-deposit-dragover', delay:100}"
317+
ngf-drop=""
318+
ngf-change="$ctrl.replaceMasterFile($newFiles, $invalidFiles)"
319+
ngf-select=""
320+
ngf-model-options="{allowInvalid: false}"
321+
ngf-max-size="500GB"
322+
ngf-multiple="true"
323+
ngf-accept="'{{$ctrl.cdsDepositsCtrl.videoExtensions}}'"
324+
ngf-pattern="'{{$ctrl.cdsDepositsCtrl.videoExtensions}}'"
325+
>
326+
<div class="pa-10 cds-deposit-box-upload-wrapper text-center">
327+
<p class="cds-deposit-box-upload-icon mb-20">
328+
<i class="fa fa-2x fa-video-camera" aria-hidden="true"></i>
329+
</p>
330+
<div class="cds-deposit-box-upload-content">
331+
<div class="cds-deposit-box-upload-title">
332+
<h4>To replace the video file, just upload a video to here.</h4>
333+
</div>
334+
<p class="cds-deposit-box-upload-description"> Or Drag & Drop files</p>
335+
</div>
336+
</div>
337+
</div>
314338
</div>
315339
</div>
316340
<div ng-if="$ctrl.cdsDepositCtrl.isPublished()" class="cds-deposit-box text-muted">
317341
<h5 class="text-muted"><strong>Tips and suggestions</strong></h5>
318342
<ul>
319343
<li>Click the <strong>Edit</strong> button on the top right corner to add more files.</li>
320344
</ul>
321-
</div>
345+
</div>

cds/modules/theme/assets/bootstrap3/js/cds_deposit/avc/components/cdsUploader.js

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ function cdsUploaderCtrl(
115115
if (!upload.key) {
116116
upload.key = upload.name;
117117
}
118-
if (that.cdsDepositsCtrl.isVideoFile(upload.key)) {
118+
if (
119+
!upload.isAdditional &&
120+
that.cdsDepositsCtrl.isVideoFile(upload.key)
121+
) {
119122
_subpromise = Upload.http(_startWorkflow(upload, response));
120123
} else {
121124
var d = $q.defer();
@@ -282,21 +285,16 @@ function cdsUploaderCtrl(
282285
angular.forEach(_files, function (file) {
283286
file.key = file.name;
284287
file.local = !file.receiver;
288+
file.isAdditional = true;
285289
// Add any extra paramemters to the files
286290
if (extraHeaders) {
287291
file.headers = extraHeaders;
288292
}
293+
file.headers = {
294+
"X-Invenio-File-Tags": "context_type=additional_file",
295+
};
289296
});
290297

291-
// Add the files to the list
292-
var masterFile = that.cdsDepositCtrl.findMasterFile() || {};
293-
var videoFiles = _.values(
294-
that.cdsDepositsCtrl.filterOutFiles(_files).videos
295-
);
296-
297-
// Exclude video files
298-
_files = _.difference(_files, videoFiles);
299-
300298
// Find if any of the existing files has been replaced
301299
// (file with same filename), and if yes remove it from the existing
302300
// file list (aka from the interface).
@@ -323,6 +321,44 @@ function cdsUploaderCtrl(
323321
Array.prototype.push.apply(that.files, _files);
324322
// Add the files to the queue
325323
Array.prototype.push.apply(that.queue, _files);
324+
325+
// Start upload automatically if the option is selected
326+
if (that.autoStartUpload) {
327+
that.upload();
328+
}
329+
};
330+
331+
this.replaceMasterFile = function (_files, invalidFiles) {
332+
// Do nothing if files array is empty
333+
if (!_files) {
334+
return;
335+
}
336+
// Remove any invalid files
337+
_files = _.difference(_files, invalidFiles || []);
338+
// Make sure they have proper metadata
339+
angular.forEach(_files, function (file) {
340+
file.key = file.name;
341+
file.local = !file.receiver;
342+
});
343+
344+
// Add the files to the list
345+
var masterFile = that.cdsDepositCtrl.findMasterFile() || {};
346+
var videoFiles = _.values(
347+
that.cdsDepositsCtrl.filterOutFiles(_files).videos
348+
);
349+
350+
if ((invalidFiles || []).length > 0) {
351+
// Push a notification
352+
toaster.pop({
353+
type: "error",
354+
title:
355+
"Invalid file(s) for " +
356+
(that.cdsDepositCtrl.record.title.title || "video."),
357+
body: _.map(invalidFiles, "name").join(", "),
358+
bodyOutputType: "trustedHtml",
359+
});
360+
}
361+
326362
if (!that.cdsDepositCtrl.master) {
327363
// Check for new master file
328364
var newMasterFile = videoFiles[0];
@@ -358,11 +394,6 @@ function cdsUploaderCtrl(
358394
});
359395
}
360396
}
361-
362-
// Start upload automatically if the option is selected
363-
if (that.autoStartUpload) {
364-
that.upload();
365-
}
366397
};
367398

368399
// Prepare file request

0 commit comments

Comments
 (0)