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 if (!$subdirectory || !$tempfile || !$timestamp || !$user_id){
98 die "To archive a tempfile you need to provide: tempfile, subdirectory, archive_filename, timestamp, archive_path, and user_id\n";
101 if (!any
{ $_ eq "curator" || $_ eq "submitter" || $_ eq "sequencer" } ($self->user_role) ) {
102 die "You have insufficient privileges to archive a file.\n". Dumper
$self->user_role;
104 # if (!$subdirectory || !$tempfile || !$archive_filename ) {
105 # print STDERR "File archive failed: incomplete information to archive file.\n";
106 # die "File archive failed: incomplete information to archive file.\n";
108 if ($self->include_timestamp){
109 $file_destination = catfile
($archive_path, $user_id, $subdirectory,$timestamp."_".$archive_filename);
112 $file_destination = catfile
($archive_path, $user_id, $subdirectory,$archive_filename);
115 if (!-d
$archive_path) {
118 if (! -d catfile
($archive_path, $user_id)) {
119 mkdir (catfile
($archive_path, $user_id));
121 if (! -d catfile
($archive_path, $user_id, $subdirectory)) {
122 mkdir (catfile
($archive_path, $user_id, $subdirectory));
124 copy
($tempfile,$file_destination);
126 $error = "Error saving archived file: $file_destination\n$_";
129 print STDERR
"$error\n";
131 print STDERR
"ARCHIVED: $file_destination\n";
132 return $file_destination;
137 my $file_name_and_location = shift;
138 #print STDERR $file_name_and_location;
140 open(my $F, "<", $file_name_and_location) || die "Can't open file $file_name_and_location";
142 my $md5 = Digest
::MD5
->new();