Rename, duplicate and move uploaded file

inchains

Joined: 2012-04-19
Posts: 18
Posted: Wed, 2012-05-09 17:02

Hi people!

Guess I need to customize the "add item" process so that once a file is uploaded it gets to do the following (in the specific order):
- rename the file;
- duplicate the renamed file;
- add a suffix to the duplicated file;
- move the file without the suffix to another dir;
- only store in db the suffix renamed file.

Exemplifying:
The user is going to upload a file named xyz.jpg. So, what I want the customization to do is:
- rename the file to its equivalent id key in the db: 1001.jpg (if id=1001);
- duplicate the file and add the suffix _n to the duplicated: got one file named 1001.jpg and another named 1001_n.jpg;
(for what I know of php in the duplicate method the file can be copied (so moved) to other dir);
- only store in the db the data related to 1001_n.jpg.

The point:
Have a repository with the "original" files that, from time to time, will be downloaded from the server and stored locally keeping only light versions (xxxx_n.jpg) on the server.

I'm open to suggestions... either pointing me to some documentation you find relevant, or helping me developing this customization or module or what ever you think it would work best.

I'm comfortable with php and mysql.

Thanx a lot.

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Wed, 2012-05-09 18:00

Gallery already builds resizes, no point in all that. Gallery keeps the original files in g2data/albums and builds the resizes(derivatives) to g2data/cache/derivative

Why not just use rsync to keep your g2data/albums directory in sync w/ your local copy.

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
inchains

Joined: 2012-04-19
Posts: 18
Posted: Thu, 2012-05-10 08:44

Cool... as I'm using it for the first time I didn't knew about that, but still, renaming the files is a must ("client" need)!

Thanx.

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Thu, 2012-05-10 17:36

Gallery does not assign the ID until the new GalleryItem creation is complete - it returns the ID upon creation.
So you'd have to setup a listener to listen for a new GalleryPhotoItem event and react by renaming the Filesystem Entity by updating the item.
codex.gallery2.org/Gallery2:API

-s
FlashYourWeb and Your Gallery with The E2 XML Media Player for Gallery2

 
inchains

Joined: 2012-04-19
Posts: 18
Posted: Thu, 2012-05-10 21:58

Ok... I'll check the url provided.

Thanks.

 
inchains

Joined: 2012-04-19
Posts: 18
Posted: Sat, 2012-05-12 18:21

Hi again... I've been busy reading lots of stuff from the api docs, but still haven't found how gallery stores and reads values in db... could you please point me to the docs where that is...

thx

 
suprsidr
suprsidr's picture

Joined: 2005-04-17
Posts: 8339
Posted: Sat, 2012-05-12 18:46

Gallery loads an item w/ this call.

    list($ret, $item) = GalleryCoreApi::loadEntitiesById($id, 'GalleryItem');
    if ($ret) {
        die("Oops something went wrong ".$ret->getAsHtml());
    }

Gallery pulls info from many DB table to assemble the $item object.

You now have access to all the $item's properties and methods.
$item->setTitle('My Cat');
$item->setDescription('this is my cool new cat');
...
including the GalleryFileSystemEntity methods what would be what you are interested in to rename the $item's path component.
$item->rename($item->getId().'.jpg');

-s

 
inchains

Joined: 2012-04-19
Posts: 18
Posted: Sat, 2012-05-12 18:58

thank you