Skip to content

Commit

Permalink
BibDocFile: addition of files redirect
Browse files Browse the repository at this point in the history
* Adds redirection on files that their record has been merged.

Signed-off-by: Harris Tzovanakis <[email protected]>
  • Loading branch information
drjova committed May 12, 2016
1 parent bd691cd commit 43edc94
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions modules/bibdocfile/lib/bibdocfile_webinterface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is part of Invenio.
# Copyright (C) 2012, 2013 CERN.
# Copyright (C) 2012, 2013, 2015, 2016 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -46,10 +46,16 @@
from invenio.webinterface_handler import wash_urlargd, WebInterfaceDirectory
from invenio.urlutils import make_canonical_urlargd, redirect_to_url
from invenio.messages import gettext_set_language
from invenio.search_engine import \
guess_primary_collection_of_a_record, get_colID, record_exists, \
create_navtrail_links, check_user_can_view_record, record_empty, \
is_user_owner_of_record
from invenio.search_engine import (
check_user_can_view_record,
create_navtrail_links,
get_colID,
get_merged_recid,
guess_primary_collection_of_a_record,
is_user_owner_of_record,
record_empty,
record_exists
)
from invenio.bibdocfile import BibRecDocs, normalize_format, file_strip_ext, \
stream_restricted_icon, BibDoc, InvenioBibDocFileError, \
get_subformat_from_format
Expand All @@ -68,6 +74,7 @@
move_uploaded_files_to_storage

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


class WebInterfaceFilesPages(WebInterfaceDirectory):
Expand Down Expand Up @@ -105,6 +112,21 @@ def getfile(req, form):
navmenuid='submit')

if record_exists(self.recid) < 1:
# Check if the record of the file has been merged
# if the /record/1 has been merged to /record/2 then
# /record/1/files/foo.pdf will redirect to /record/2/files/foo.pdf
merged_recid = get_merged_recid(self.recid)
if merged_recid:
# Build the url for the file in merged record
url = "{0}/{1}/{2}/files/{3}".format(
CFG_SITE_URL,
CFG_SITE_RECORD,
merged_recid,
filename
)
return redirect_to_url(
req, url, redirection_type=apache.HTTP_MOVED_PERMANENTLY
)
msg = "<p>%s</p>" % _("Requested record does not seem to exist.")
return warning_page(msg, req, ln)

Expand Down Expand Up @@ -170,6 +192,12 @@ def getfile(req, form):
else:
docname = args['docname']

try:
obelix.log('download_after_search',
clean_user_info(user_info), self.recid)
except Exception:
register_exception(alert_admin=True)

if not docformat:
docformat = args['format']
if args['subformat']:
Expand Down Expand Up @@ -225,7 +253,7 @@ def getfile(req, form):
if not docfile.hidden_p():
if not readonly:
ip = str(req.remote_ip)
doc.register_download(ip, docfile.get_version(), docformat, uid, self.recid)
doc.register_download(ip, docfile.get_version(), docformat, req.headers_in.get('User-Agent'), uid, self.recid)
try:
return docfile.stream(req, download=is_download)
except InvenioBibDocFileError, msg:
Expand Down

0 comments on commit 43edc94

Please sign in to comment.