file name regex

70 views
Skip to first unread message

Franziska Rapp

unread,
Nov 7, 2023, 7:21:46 AM11/7/23
to DSpace Technical Support

Hi,

We'd like to introduce restrictions on the file name during submission:

Our modification:
<form name="bitstream-metadata">
            <row>
                <field>
                    <dc-schema>dc</dc-schema>
                    <dc-element>title</dc-element>
                    <dc-qualifier></dc-qualifier>
                    <repeatable>false</repeatable>
                    <label>Title</label>
                    <input-type>onebox</input-type>
                    <hint>Enter the name of the file.</hint>
                    <required>You must enter a name for this file</required>
					<regex>\w{1,20}</regex>
                </field>
            </row>
            <row>
                <field>
                    <dc-schema>dc</dc-schema>
                    <dc-element>description</dc-element>
                    <repeatable>true</repeatable>
                    <label>Description</label>
                    <input-type>textarea</input-type>
                    <hint>Enter a description for the file</hint>
                    <required></required>
                </field>
            </row>
        </form>

-- 
Franziska Rapp

Academic Librarian
Communication and Information Center (kiz)
Ulm University

Manuele Lorusso

unread,
Nov 7, 2023, 11:48:28 AM11/7/23
to dspac...@googlegroups.com

Hi Franziska,

you have to begin your regex with a start- and endtoken (^ and $) otherwise it is like a have to contains expression.

In your case the regex must be as follows:

        ^\w{1,20}$


Best,
Manuele

Herr Manuele Lorusso
Softwareentwickler / Abt. IT-Dienste Forschung, Lehre, Infrastruktur
Kommunikations-, Informations-, Medienzentrum (KIM)
Universität Konstanz
78457 Konstanz
+49 (0)7531 / 88-5403
Raum B 707
--
All messages to this mailing list should adhere to the Code of Conduct: https://www.lyrasis.org/about/Pages/Code-of-Conduct.aspx
---
You received this message because you are subscribed to the Google Groups "DSpace Technical Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dspace-tech...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dspace-tech/51806b1b-56f0-46fa-9c23-688a96a343fa%40uni-ulm.de.

Franziska Rapp

unread,
Nov 8, 2023, 3:50:15 AM11/8/23
to dspac...@googlegroups.com

Hi Manuele, hi All,

Thank you very much for your help.

It solved our problem to some part: When you edit the file name, the restrictions are in place.


I have three follow-up points:

1) The check is not in place during the file upload (submission). That means users can upload files that don't meet the file name criteria. Any ideas how to solve this?

2) The users should get a more meaningful feedback. Is there a way to translate regex into user friendly words and display that instead?

3) Is this example outdated and should be corrected? https://github.com/DSpace/DSpace/blob/58ea388d6ac623575835cfa3be37ff7de59a98a2/dspace/config/submission-forms.xml#L265


Best regards

Franziska

Max Nuding

unread,
Nov 8, 2023, 4:10:17 AM11/8/23
to dspac...@googlegroups.com, Manuele Lorusso, franzis...@uni-ulm.de
Hi Franziska,

1) This cannot be done by configuration alone, you'll need to change the
validation code in UploadValidation.java

2) We've had this very same issue. There isn't a *good* solution for
this, but a few workaround/options:

If you change the validation code in the backend you can throw a custom
error and then write a validation in the frontend to do the same (and
remove the regex). This is extra work and not very well documented but
should work without issues. Another solution works only if the file name
is the *only* place you're doing regex validation: Just change the
message key in de.json and/or en.json to something different. But this
would apply to all regexvalidations.


Best

Max

On 08.11.23 09:50, Franziska Rapp wrote:
>
> Hi Manuele, hi All,
>
> Thank you very much for your help.
>
> It solved our problem to some part: When you edit the file name, the
> restrictions are in place.
>
>
>>> <https://groups.google.com/d/msgid/dspace-tech/51806b1b-56f0-46fa-9c23-688a96a343fa%40uni-ulm.de?utm_medium=email&utm_source=footer>.
>> --
>> All messages to this mailing list should adhere to the Code of
>> Conduct: https://www.lyrasis.org/about/Pages/Code-of-Conduct.aspx
>> ---
>> You received this message because you are subscribed to the Google
>> Groups "DSpace Technical Support" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to dspace-tech...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/dspace-tech/849737ea-006a-42ff-ad0e-269fa8528675%40uni-konstanz.de
>> <https://groups.google.com/d/msgid/dspace-tech/849737ea-006a-42ff-ad0e-269fa8528675%40uni-konstanz.de?utm_medium=email&utm_source=footer>.
> --
> --
> All messages to this mailing list should adhere to the Code of
> Conduct: https://www.lyrasis.org/about/Pages/Code-of-Conduct.aspx
> ---
> You received this message because you are subscribed to the Google
> Groups "DSpace Technical Support" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to dspace-tech...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/dspace-tech/b4d895a9-9a4c-48a4-9bda-b474782d44dc%40uni-ulm.de
> <https://groups.google.com/d/msgid/dspace-tech/b4d895a9-9a4c-48a4-9bda-b474782d44dc%40uni-ulm.de?utm_medium=email&utm_source=footer>.

--
Herr Max Nuding
Softwareentwickler / Abt. IT-Dienste für Forschung und Lehre
Kommunikations-, Informations-, Medienzentrum (KIM)
Universität Konstanz
78467 Konstanz
Tel: +49.7531.88-4658
Raum: B707

Franziska Rapp

unread,
Nov 8, 2023, 5:58:41 AM11/8/23
to Max Nuding, dspac...@googlegroups.com, Manuele Lorusso
Hi Max, hi All,

Thank you, that's helpful. Could you provide examples for 1) and 2) ?

Also, what is the best way to proceed when we'd like to work towards a
solution for the official DSpace release?

Best regards
Franziska

Max Nuding

unread,
Nov 8, 2023, 6:45:15 AM11/8/23
to Franziska Rapp, dspac...@googlegroups.com
Hi Franziska,

for 1), in UploadValidation.java in "validate" right above "return errors":

var bundles = itemService.getBundles(obj.getItem(), "ORIGINAL");
if (!bundles.isEmpty()) {
var bundle = bundles.get(0);
var bitstreams = bundle.getBitstreams();
if (!bitstreams.isEmpty()) {
var bitstream = bitstreams.get(bitstreams.size() - 1);
if (!Pattern.matches("^\\w{1,20}$", bitstream.getName())) {
addError(
errors,
"error.validation.filename",
"/" +
WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS +
"/" +
config.getId());
}
}
}

This is very quick and dirty and just the general gist of it. We also
cannot validate the freshly uploaded file here, so either validate all
existing filenames or (as done here) just validate the newest one.

You could also edit UploadStep.java in the upload() Method. As first
lines within the try block add (again, quick and dirty solution, also
use a better error message)

var filename = Utils.getFileName(file);
if (!Pattern.matches("^\\w{1,20}$", filename)) {
throw new IllegalArgumentException("Invalid file name!");
}

But I *think* the frontend just shows a generic "an error occured"
message instead of an actually useful error message if you do that.

2) I don't have something to throw in here for that, sorry. Custom
validators aren't documented, so we just took a few guesses on what to
do have have code sprinkled all over the place for this.

> Also, what is the best way to proceed when we'd like to work towards a
> solution for the official DSpace release?

Create an issue in https://github.com/DSpace/dspace

Best
Max
--
Herr Max Nuding
Softwareentwickler / Abt. IT-Dienste für Forschung und Lehre
Kommunikations-, Informations-, Medienzentrum (KIM)
Universität Konstanz
78457 Konstanz
Tel.: +49.7531.88-4658
Raum: B 707

Franziska Rapp

unread,
Nov 8, 2023, 7:46:10 AM11/8/23
to Max Nuding, dspac...@googlegroups.com

Hi,

I created two new issues (feature requests):

https://github.com/DSpace/DSpace/issues/9177

https://github.com/DSpace/DSpace/issues/9178

Feel free to add your comments and ideas.

Best regards
Franziska

Reply all
Reply to author
Forward
0 new messages