11# This file is part of Invenio.
2- # Copyright (C) 2012, 2013, 2015 CERN.
2+ # Copyright (C) 2012, 2013, 2015, 2016, 2017 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
4646from invenio .webinterface_handler import wash_urlargd , WebInterfaceDirectory
4747from invenio .urlutils import make_canonical_urlargd , redirect_to_url
4848from 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_fieldvalues ,
54+ get_merged_recid ,
55+ guess_primary_collection_of_a_record ,
56+ is_user_owner_of_record ,
57+ record_empty ,
58+ record_exists
59+ )
5360from invenio .bibdocfile import BibRecDocs , normalize_format , file_strip_ext , \
5461 stream_restricted_icon , BibDoc , InvenioBibDocFileError , \
5562 get_subformat_from_format
7077bibdocfile_templates = invenio .template .load ('bibdocfile' )
7178from invenio .obelixutils import obelix , clean_user_info
7279
80+ # If the 980__a contains any of the values it will redirect any of the
81+ # merged files to the first available file of the new record
82+ REDIRECT_TO_FIRST_FILE = ['OLDBROCHURE' , 'BROCHURE' , ]
7383
7484class WebInterfaceFilesPages (WebInterfaceDirectory ):
7585
@@ -82,13 +92,14 @@ def _lookup(self, component, path):
8292 filename = component
8393
8494 def getfile (req , form ):
95+
8596 args = wash_urlargd (form , bibdocfile_templates .files_default_urlargd )
8697 ln = args ['ln' ]
8798 if filename [:9 ] == "allfiles-" :
88- files_size = filename [9 :]
99+ filesizes = filename [9 :]
89100 # stream a tar package to the user
90101 brd = BibRecDocs (self .recid )
91- brd .stream_archive_of_latest_files (req , files_size )
102+ brd .stream_archive_of_latest_files (req , filesizes )
92103 return
93104
94105 _ = gettext_set_language (ln )
@@ -106,6 +117,35 @@ def getfile(req, form):
106117 navmenuid = 'submit' )
107118
108119 if record_exists (self .recid ) < 1 :
120+ # Check if the record of the file has been merged
121+ # if the /record/1 has been merged to /record/2 then
122+ # /record/1/files/foo.pdf will redirect to /record/2/files/foo.pdf
123+ merged_recid = get_merged_recid (self .recid )
124+ if merged_recid :
125+ _filename = filename
126+ try :
127+ # If the 980__a matches the REDIRECT_TO_FIRST_FILE
128+ _collections = get_fieldvalues (self .recid , '980__a' )
129+ if any (x in REDIRECT_TO_FIRST_FILE for x in _collections ):
130+ # Get the first file from the new record
131+ _bib = BibRecDocs (merged_recid )
132+ # Find the filename of the first bibdocfile
133+ _filename = "{0}.{1}" .format (
134+ _bib .get_bibdoc_names ()[0 ],
135+ filename [- 3 :]
136+ )
137+ except Exception :
138+ pass
139+ # Build the url for the file in merged record
140+ url = "{0}/{1}/{2}/files/{3}" .format (
141+ CFG_SITE_URL ,
142+ CFG_SITE_RECORD ,
143+ merged_recid ,
144+ _filename
145+ )
146+ return redirect_to_url (
147+ req , url , redirection_type = apache .HTTP_MOVED_PERMANENTLY
148+ )
109149 msg = "<p>%s</p>" % _ ("Requested record does not seem to exist." )
110150 return warning_page (msg , req , ln )
111151
0 commit comments