1 package CXGN
::UploadFile
;
5 CXGN::UploadFile - an object to handle uploading files
9 my $uploader = CXGN::UploadFile->new({
10 tempfile => '/tmp/myfile.csv',
11 subdirectory => 'some_directory',
12 archive_path => '/some/path/to/dir',
13 archive_filename => 'myfilename.csv',
14 timestamp => '2016-09-24_10:30:30',
16 user_role => 'curator'
18 my $uploaded_file = $uploader->archive();
19 my $md5 = $uploader->get_md5($uploaded_file);
21 In this example, the tempfile myfile.csv will be saved in: /some/path/to/dir/41/some_directory/2016-09-24_10:30:30_myfilename.csv
28 Jeremy D. Edwards (jde22@cornell.edu)
36 use List
::MoreUtils qw
/any /;
38 use File
::Spec
::Functions
;
39 use File
::Basename qw
| basename dirname
|;
43 has
'tempfile' => (isa
=> "Str",
48 has
'subdirectory' => (isa
=> "Str",
53 has
'archive_path' => (isa
=> "Str",
58 has
'archive_filename' => (isa
=> "Str",
63 has
'timestamp' => (isa
=> "Str",
68 has
'user_id' => (isa
=> "Int",
73 has
'user_role' => (isa
=> "Str",
78 has
'include_timestamp' => (
87 my $subdirectory = $self->subdirectory;
88 my $tempfile = $self->tempfile;
89 my $archive_filename = $self->archive_filename;
90 my $timestamp = $self->timestamp;
91 my $archive_path = $self->archive_path;
92 my $user_id = $self->user_id;
96 if (!$subdirectory || !$tempfile || !$archive_filename || !$timestamp || !$archive_path || !$user_id){
97 die "To archive a tempfile you need to provide: tempfile, subdirectory, archive_filename, timestamp, archive_path, and user_id\n";
100 if (!any
{ $_ eq "curator" || $_ eq "submitter" || $_ eq "sequencer" } ($self->user_role) ) {
101 die "You have insufficient privileges to archive a file.\n". Dumper
$self->user_role;
103 if (!$subdirectory || !$tempfile || !$archive_filename ) {
104 print STDERR
"File archive failed: incomplete information to archive file.\n";
105 die "File archive failed: incomplete information to archive file.\n";
107 if ($self->include_timestamp){
108 $file_destination = catfile
($archive_path, $user_id, $subdirectory,$timestamp."_".$archive_filename);
111 $file_destination = catfile
($archive_path, $user_id, $subdirectory,$archive_filename);
114 if (!-d
$archive_path) {
117 if (! -d catfile
($archive_path, $user_id)) {
118 mkdir (catfile
($archive_path, $user_id));
120 if (! -d catfile
($archive_path, $user_id, $subdirectory)) {
121 mkdir (catfile
($archive_path, $user_id, $subdirectory));
123 copy
($tempfile,$file_destination);
125 $error = "Error saving archived file: $file_destination\n$_";
130 print STDERR
"ARCHIVED: $file_destination\n";
131 return $file_destination;
136 my $file_name_and_location = shift;
137 #print STDERR $file_name_and_location;
139 open(my $F, "<", $file_name_and_location) || die "Can't open file ";
141 my $md5 = Digest
::MD5
->new();