dynamic folder in upload file

Wish to see a specific feature/change in future releases? Feel free to post it here, and if it gets enough "likes", we'd definitely include it in future releases!
Post Reply
User avatar
landinialejandro
AppGini Super Hero
AppGini Super Hero
Posts: 126
Joined: 2016-03-06 00:59
Location: Argentina
Contact:

dynamic folder in upload file

Post by landinialejandro » 2022-03-17 16:44

Hello there.
I found myself needing to be able to dynamically change the location where each file will be uploaded in a form, for example images/tablename/id.
This is possible if you could modify the 'dir' variable of the fileUpload function.
But it is not possible for me unless it is written within the base code of the application, which would be modified each time it is compiled.
I also did not find a way to pass it by parameter from the hooks functions.

On the other hand, I also found that the folder was not generated for me, and I found that the mkdir function of the PrepareUploadedFile function was missing the recursion true.

I think that it is not very far from being able to solve the first point, to be able to pass the folder by parameter.
But the second point I think can be solved with the next version

Greetings Alejandro.
Alejandro.
AppGini 5.98 - Linux OpenSuse Tumblewweed.

Some of my posts that may interest you:
:arrow: Landini Admin Template: Template for Appgini like AdminLTE
:arrow: Profile image plugin: add and changue image user profile
:arrow: Field editor in table view: Configurable fast edit fields in TV
:idea: my personal page

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: dynamic folder in upload file

Post by jsetzer » 2022-03-17 17:02

Why not use before* hooks, create subfolder depending on PK and change filename in DB?
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
landinialejandro
AppGini Super Hero
AppGini Super Hero
Posts: 126
Joined: 2016-03-06 00:59
Location: Argentina
Contact:

Re: dynamic folder in upload file

Post by landinialejandro » 2022-03-17 17:21

Hi Jan, I'm going to try that, but it seems a bit far-fetched to me, maybe it's better in the after hook, so appgini finishes all its upload process, and I move the file to the folder I need. maybe... hahaha.
Although I still think that the suggestion could work better.
Thanks for your support.

Saludos Alejandro
Alejandro.
AppGini 5.98 - Linux OpenSuse Tumblewweed.

Some of my posts that may interest you:
:arrow: Landini Admin Template: Template for Appgini like AdminLTE
:arrow: Profile image plugin: add and changue image user profile
:arrow: Field editor in table view: Configurable fast edit fields in TV
:idea: my personal page

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: dynamic folder in upload file

Post by jsetzer » 2022-03-17 17:59

Both should be possible. If you intercept before* you will have access to $_FILES array, additionally. For example I am using before_insert hook for fetching the file's original filename and storing it in DB. Having the (unchanged) filename in DB next to the generated unique filename helps my users finding attachments using built-in search function. So, before* can bring some benefit. But you are right: both should work.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: dynamic folder in upload file

Post by onoehring » 2024-02-28 10:39

Hi,

I am not sure, if the problem was solved - however, I do have the same need now:
In the generic upload folder are currently almost 20.000 files: documents and images.
I would like to split this into subfolders. e.g. if the regular upload folder is /images, if the uploaded file has the primary key "3" (all PKs are autonumber), the uploaded file should reside in /images/003. If the pk is 456124 the file would be in /images/124:
Take a right-padded PK and create a subfolder. Of course, this means upload with PK 14003 would also reside in /images/003 - that's fine.

After some experiments, I notice, that I COULD add the folder to the fieldname. Example: Fieldname that holds the name to the uploaded file as it it saved is "myDocument".

Code: Select all

pk I myDocument
---------------------
4  I 09712CAEF31.pdf
would become (simply using after_insert/after_update hooks)

Code: Select all

pk I myDocument
---------------------
4  I 004/09712CAEF31.pdf
notice the new prefix 004/ of the filename.
This seems to work for documents (files that are NOT images)

For images let's make another example:

Code: Select all

pk I myImage
---------------------
17  I AB2134F91.jpg
would become

Code: Select all

pk I myImage
---------------------
17  I 017/AB2134F91.jpg
It seems to work for images - BUT not for the thumbnails. Even when the thumbnails 017/AB2134F91_tv.jpg and 017/AB2134F91_dv.jpg exist (in the subdirectory) - the thumbnails are not displayed, just the general placeholder is shown.
I created a sample application and allowed to zoom the image (setting in AG). Thus clicking on the (mistakenly displayed) placeholder zooms the CORRECT image.

Side-Question:
I have not yet looked into when the thumbnails are being generated: Is that before or after the "after_..." - hook?

Thus my question:
How can a simple code be added that is universally working for all uploads (also images with their thumbnails)?
Of course it's preferred, that this is code (a file) that not once files that are being regenerated.

Thanks a lot for any help
Olaf

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: dynamic folder in upload file

Post by jsetzer » 2024-03-08 15:56

A few years ago someone from scandinavia told me about performance problems with directories having >10000 files - even in Linux.

In a different project for them I have auto created subdirectories per master record, moved uploaded files there and changed the path, stored in DB, to reflect the new storage location.

You may consider something like that.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: dynamic folder in upload file

Post by onoehring » 2024-03-09 10:04

Hi Jan,

thank you for your answer.
Indeed, the uploads folder has almost 18000 files now. It's uploaded PDFs and images (jpg), the latter of course with _dv and _tv alternatives (total filenumber is around 18000).
Your suggestion seems to me like my

Code: Select all

pk I myDocument
---------------------
4  I 004/09712CAEF31.pdf
where I add the id as file prefix and place the file in the subfolder /uploads/004/09712CAEF31.pdf .
For PDFs this works as described. Unfortunately image previews (_dv and _tv) do not work. Once I click on the "broken" preview icon, the image can be displayed without problems.

I suppose the problem is in /thumbnails.php:
In thumbnails the upload dir gets pulled from incfunctions.php -> getUploadDir() . This seems to return the regular upload dir with a slash in the end. So far, so good.
Currently I am trying to figure out if I "only" need to change thumnail.php ... to fix the display of the thumbs.
Once this works, "moving" files with the after_ hooks (described in my previous post) should work.

EDIT 1:
in /thumbnails.php
changing

Code: Select all

if (!preg_match('/^[a-z0-9_-]+\.(jpg|jpeg|gif|png|webp)$/i', $i, $m)) {
to

Code: Select all

if (!preg_match('/^[a-z0-9_-_\/]+\.(jpg|jpeg|gif|png|webp)$/i', $i, $m)) {
will handle subfolders fine ... for displaying.
Currently no generated thumbs are removed when the main image file is deleted from the server. I need to investigate this

Olaf

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: dynamic folder in upload file

Post by onoehring » 2024-03-21 14:05

Hi Jan and landinialejandro and all others,

please see my teaser (soon: solution) here: viewtopic.php?f=12&t=5349

Olaf

Post Reply