EPrints Technical Mailing List Archive

See the EPrints wiki for instructions on how to join this mailing list and related information.

Message: #10197


< Previous (by date) | Next (by date) > | < Previous (in thread) | Next (in thread) > | Messages - Most Recent First | Threads - Most Recent First

RE: [EP-tech] Renaming files


CAUTION: This e-mail originated outside the University of Southampton.

Hi David,

 

Unfortunately this is not exactly what we need, we want filenames to be derived from eprint metadata.

 

I kinda succeeded to get the files to be renamed correctly, but I stille have some issues.

 

First setting $basename to a string taken from metadata through eprint_fields_automatic then


            if ($basename) {

                                       

                my $new_filename = $basename . $extension;

 

                my $fileobj = $doc->get_value("files")->[0];

                return unless $fileobj;

 

                my $old_path = $fileobj->get_local_copy;

 

                unless ( -e $old_path ) {

                    print $fh "File not found : $old_path";

                    return;

                }

 

                my $newfile = $doc->add_file(

                    $old_path,

                    $new_filename

                );

 

                print $fh "success: $newfile\n";

 

                if ($newfile) {

                    my $files = $doc->get_value("files");

                    $doc->set_main($new_filename);

                    $fileobj->remove();

 

                    $doc->commit();

                }

            }

 

The problem with this is that when $fileobj->remove() is called, both files are deleted, while just the first one should be deleted. I need to test it more thoroughly to make sure files don’t get deleted especially when this is called for various reasons…

 

Robin

 

De : David R Newman <drn@ecs.soton.ac.uk>
Envoyé : 4 août 2025 13:37
À : eprints-tech@ecs.soton.ac.uk; Robin Sylvestre <robin.sylvestre@polymtl.ca>
Objet : Re: [EP-tech] Renaming files

 

Hi Robin,

I am not aware of any conventional way to do this.  It has not been a requirement I am aware of anyone else requiring, except for maybe better protection from using filenames that either are invalid in the filesystem or liable to cause different values being stored in the database and filesystem.

I did add a configuration option to EPrints:

$c->{generic_filenames} = 1;

For the purpose of better protection against unusual filenames.  With this setting on, all files would be stored in the same path as before but with the filename <fileid>.bin.  Although you could change this for a pre-existent repository, it is more intended for new repositories, as having a mish-mash of different ways of setting the filename may have unintended consequences, which are difficult to test pre-emptively.  

The way generic_filenames has been implemented, files would still be served as the filename they were uploaded but you only need worry about changing the name of the file in the database rather than needing code that would also update the filename in the filesystem.  Therefore, it may be easier to get closer to your solution using this feature.  However, as the filename on disk needs to either match the filename in the database or <fileid>.bin, then this is not really suited for an existing repository, as you would need to manually update all existing documents to meet the <fileid>.bin filename format.

Regards

David Newman

 

On 04/08/2025 17:04, Robin Sylvestre wrote:

CAUTION: This e-mail originated outside the University of Southampton.

CAUTION: This e-mail originated outside the University of Southampton.

Hi everyone,

 

We would like to rename files automatically when they are deposited, following a naming convention. I tried to work with the sanitize option but I had two major issues : the first one is that this option doesn’t give access natively to either the eprint or the doc objects. I can derive the eprintid from the http request, but the other issue is that this filter is applied when the file is first uploaded, meaning that basic metadata of the document is not set yet, and some of the data is required to rename files accordingly.

 

The renaming has to be applied when the document is deposited and all of its metadata is filled. The documentation for the document or the file objects doesn’t include any rename function and I haven’t been able to find a way to apply the filter from the document_fields_automatic functions either. I supposed I could create a new file with the new name and remove the old one, but all my attempts have been unsuccessful.

 

Any help?

 

Thank you!

 

Robin Sylvestre
Technicien des systèmes informatisés
Bibliothèque Louise-Lalonde-Lamarre
Polytechnique Montréal
514-340-4711 poste 5973

 



*** Options: https://wiki.eprints.org/w/Eprints-tech_Mailing_List
*** Archive: https://www.eprints.org/tech.php/
*** EPrints community wiki: https://wiki.eprints.org/