Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Getting uploaded file details from database?

Quote Reply
Getting uploaded file details from database?
I'm trying to create a plugin that first needs the path and filename of a file that a user has uploaded. In the database this is stored as a file with hashed method. What I will be doing with the file is using it as an input in FFmpeg and returning some data to input into other fields and converting it into FLV. Any help with that would be very much appreciated.
Quote Reply
Re: [anthonyweston] Getting uploaded file details from database? In reply to
Hi,

Check out my Get_Image_URL() plugin in the ULTRAGlobals plugin (linked in my signature). It can easily be used to grab the path as well, by simply changing:

Code:
my $file_url = $image_details->{File_URL}; # this is only the URL/folder

...to:

Code:
my $file_url = $image_details->{File_Directory}; # this is only the URL/folder

Also, if you are using this function - you can trim this line down from:

Code:
my @cut = split //, $id;
my $folderid = $cut[$#cut];

...to:

Code:
my $folderid = (reverse split //, $id)[0];

I keep meaning to update that code in the plugin (and a few other things), but never seem to find the time to do it Smile

Hope that helps

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Post deleted by anthonyweston In reply to

Last edited by:

anthonyweston: Oct 27, 2011, 5:22 AM
Quote Reply
Re: [Andy] Getting uploaded file details from database? In reply to
Thanks for replying so quick Andy.


I have done some code. I'm very very new to Perl, so I have no idea whether this will work, but I've taken your suggestion and tried to build it in. If you could point me in the right direction, that would be awesome.

Code:
#!/usr/bin/perl



use strict;
use lib '/var/www/vhosts/biohazardmen.com/cgi-bin/tube/admin';
use Links qw/$DB $IN $CFG/;
use LWP::Simple;


my $tbl = $DB->table('Links');
my $sth = $tbl->select({ID) || die $GT::SQL::error;
my $hit = $sth->fetchrow_hashref;
my $file_url = $video_path->{File_Directory};
my @split = split /\//,$hit->{Video};
my $filename = $split[$#split];
my $input_video = " $file_url . $filename ";


`ffmpeg -i $input_video -ab 56 -ar 44100 -b 200 -r 15 -s 640x480 -f flv $flv_video`;
`ffmpeg -i $input_video -vcodec mjpeg -vframes 1 -an -f rawvideo -ss 00:00:10 -s 160*120 $thumb_image`;
`ffmpeg -i $input_video -vcodec mjpeg -vframes 1 -an -f rawvideo -ss 00:00:20 -s 640*480 $video_image`;
my $runtime = `ffmpeg -i $input_video 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//`;
$runtime =~ s/\.\d\d$// ;


$DB->table('Links')->update( { FLV_File => $flv_video, Length => $runtime, Thumb_160x120 => $thumb_image, Video_Image => $video_image || \"NULL" } , {ID} );


`rm -f $input_video`;


Just to explain what I'm trying to do above...


This will be a plugin that is run when the user adds a link (video in my case), so I think the hook is add_link?


The script gets the video file that the user uploaded, converts it to FLV, generates an image and thumb and gets the runtime, and inserts the outputs to the database. Then deletes the original file that the user created. Where I have most difficulty is finding how to make the output files be named with the link ID. For instance, if the link ID is 5000 then the outputs would be 5000.flv and 5000.jpg etc. The fields in the database for the outputted files are of file type (hashed).


Am I even anywhere close with what I have?
Quote Reply
Re: [anthonyweston] Getting uploaded file details from database? In reply to
Hi,

Yeah, I would add a new field to the glinks_Links table called "isProcessed". I would do it something like this:

Code:
#!/usr/bin/perl

use strict;
use lib '/var/www/vhosts/biohazardmen.com/cgi-bin/tube/admin';
use Links qw/$DB $IN $CFG/;
use LWP::Simple;

my $tbl = $DB->table('Links');
my $sth = $tbl->select( { isProcessed => 0 } ) || die $GT::SQL::error;

while (my $hit = $sth->fetchrow_hashref) {
processed_record($hit);
}

sub processed_record {

my $video_path = Get_Image_URL($hit->{ID},'Video','Links');

#my @split = split /\//,$hit->{Video};
my $filename = (reverse split /\//, $video_path)[0];

my $dest_flv = $filename;
$dest_flv =~ s/\.(mpe?4|mpe?g|mov)/.flv/i; # be sure to add any other formats you want to accept in here. It will change stuff like test.mp4 into test.flv

my $thumb_image = "/path/to/your/thumbs/$hit->{ID}.jpg";
my $large_image = "/path/to/your/full/$hit->{ID}.jpg";

`ffmpeg -i $video_path -ab 56 -ar 44100 -b 200 -r 15 -s 640x480 -f flv $dest_flv`;
`ffmpeg -i $video_path -vcodec mjpeg -vframes 1 -an -f rawvideo -ss 00:00:10 -s 160*120 $thumb_image`;
`ffmpeg -i $video_path -vcodec mjpeg -vframes 1 -an -f rawvideo -ss 00:00:20 -s 640*480 $large_image`;
my $runtime = `ffmpeg -i $video_path 2>&1 | grep "Duration" | cut -d ' ' -f 4 | sed s/,//`;
$runtime =~ s/\.\d\d$// ;

$DB->table('Links')->update( {
FLV_File => $dest_flv,
Length => $runtime,
Thumb_160x120 => $thumb_image,
Video_Image => $large_image,
isProcessed => 1
} , { ID => $hit->{ID} } ) || die $GT::SQL::error;

`rm -f $video_path`; # be cautious with this one - in case you ever wanna "reprocess" the videos ;)
}


sub Get_Image_URL {


my ($ID,$field,$table) = @_;

$table ||= 'Links';

# make sure a fieldname and ID are provided...
if (!$ID || !$field) { return "You need to define the ID and fieldname."; }

# get the actual path to where the file is/will be saved...
my $schema = $DB->table($table)->cols;
my $path = $schema->{$field}->{'file_save_url'};
$path =~ s,/$,,; # get rid of trailing / at end of $path

my $image_details = $DB->table($table."_Files")->select( { ForeignColName => $field, ForeignColKey => $ID } )->fetchrow_hashref;

my $id = $image_details->{ID};
my $filename = $image_details->{File_Name};
my $file_url = $image_details->{File_Directory}; # this is only the URL/folder

my $folderid = (reverse split //, $id)[0];

my $url = "$file_url/$folderid/$id-$filename";


$url =~ s|([^\/]+)$|GT::CGI->escape( $1 )|e;

return ($url);

}

Totally untested, but hopefully it gives you an idea (I can't really do much more, without doing the job Smile)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Getting uploaded file details from database? In reply to
Thanks! I'm inching closer, but don't think I've quite got it. I've emailed you now to see if you can help.

Cheers!

Anthony