Skip to content

Commit 43edc94

Browse files
committed
BibDocFile: addition of files redirect
* Adds redirection on files that their record has been merged. Signed-off-by: Harris Tzovanakis <me@drjova.com>
1 parent bd691cd commit 43edc94

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

modules/bibdocfile/lib/bibdocfile_webinterface.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file is part of Invenio.
2-
# Copyright (C) 2012, 2013 CERN.
2+
# Copyright (C) 2012, 2013, 2015, 2016 CERN.
33
#
44
# Invenio is free software; you can redistribute it and/or
55
# modify it under the terms of the GNU General Public License as
@@ -46,10 +46,16 @@
4646
from invenio.webinterface_handler import wash_urlargd, WebInterfaceDirectory
4747
from invenio.urlutils import make_canonical_urlargd, redirect_to_url
4848
from invenio.messages import gettext_set_language
49-
from invenio.search_engine import \
50-
guess_primary_collection_of_a_record, get_colID, record_exists, \
51-
create_navtrail_links, check_user_can_view_record, record_empty, \
52-
is_user_owner_of_record
49+
from invenio.search_engine import (
50+
check_user_can_view_record,
51+
create_navtrail_links,
52+
get_colID,
53+
get_merged_recid,
54+
guess_primary_collection_of_a_record,
55+
is_user_owner_of_record,
56+
record_empty,
57+
record_exists
58+
)
5359
from invenio.bibdocfile import BibRecDocs, normalize_format, file_strip_ext, \
5460
stream_restricted_icon, BibDoc, InvenioBibDocFileError, \
5561
get_subformat_from_format
@@ -68,6 +74,7 @@
6874
move_uploaded_files_to_storage
6975

7076
bibdocfile_templates = invenio.template.load('bibdocfile')
77+
from invenio.obelixutils import obelix, clean_user_info
7178

7279

7380
class WebInterfaceFilesPages(WebInterfaceDirectory):
@@ -105,6 +112,21 @@ def getfile(req, form):
105112
navmenuid='submit')
106113

107114
if record_exists(self.recid) < 1:
115+
# Check if the record of the file has been merged
116+
# if the /record/1 has been merged to /record/2 then
117+
# /record/1/files/foo.pdf will redirect to /record/2/files/foo.pdf
118+
merged_recid = get_merged_recid(self.recid)
119+
if merged_recid:
120+
# Build the url for the file in merged record
121+
url = "{0}/{1}/{2}/files/{3}".format(
122+
CFG_SITE_URL,
123+
CFG_SITE_RECORD,
124+
merged_recid,
125+
filename
126+
)
127+
return redirect_to_url(
128+
req, url, redirection_type=apache.HTTP_MOVED_PERMANENTLY
129+
)
108130
msg = "<p>%s</p>" % _("Requested record does not seem to exist.")
109131
return warning_page(msg, req, ln)
110132

@@ -170,6 +192,12 @@ def getfile(req, form):
170192
else:
171193
docname = args['docname']
172194

195+
try:
196+
obelix.log('download_after_search',
197+
clean_user_info(user_info), self.recid)
198+
except Exception:
199+
register_exception(alert_admin=True)
200+
173201
if not docformat:
174202
docformat = args['format']
175203
if args['subformat']:
@@ -225,7 +253,7 @@ def getfile(req, form):
225253
if not docfile.hidden_p():
226254
if not readonly:
227255
ip = str(req.remote_ip)
228-
doc.register_download(ip, docfile.get_version(), docformat, uid, self.recid)
256+
doc.register_download(ip, docfile.get_version(), docformat, req.headers_in.get('User-Agent'), uid, self.recid)
229257
try:
230258
return docfile.stream(req, download=is_download)
231259
except InvenioBibDocFileError, msg:

0 commit comments

Comments
 (0)