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 second_subdirectory => 'some_directory',
13 archive_path => '/some/path/to/dir',
14 archive_filename => 'myfilename.csv',
15 timestamp => '2016-09-24_10:30:30',
17 user_role => 'curator'
19 my $uploaded_file = $uploader->archive();
20 my $md5 = $uploader->get_md5($uploaded_file);
22 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
29 Jeremy D. Edwards (jde22@cornell.edu)
37 use List
::MoreUtils qw
/any /;
39 use File
::Spec
::Functions
;
40 use File
::Basename qw
| basename dirname
|;
44 has
'tempfile' => (isa
=> "Str",
49 has
'subdirectory' => (isa
=> "Str",
54 has
'second_subdirectory' => (isa
=> "Str",
59 has
'third_subdirectory' => (isa
=> "Str",
64 has
'archive_path' => (isa
=> "Str",
69 has
'archive_filename' => (isa
=> "Str",
74 has
'timestamp' => (isa
=> "Str",
79 has
'user_id' => (isa
=> "Int",
84 has
'user_role' => (isa
=> "Str",
89 has
'include_timestamp' => (
98 my $subdirectory = $self->subdirectory;
99 my $second_subdirectory = $self->second_subdirectory;
100 my $third_subdirectory = $self->third_subdirectory;
101 my $tempfile = $self->tempfile;
102 my $archive_filename = $self->archive_filename;
103 my $timestamp = $self->timestamp;
104 my $archive_path = $self->archive_path;
105 my $user_id = $self->user_id;
106 my $file_destination;
109 # if (!$subdirectory || !$tempfile || !$archive_filename || !$timestamp || !$archive_path || !$user_id){
110 if (!$subdirectory || !$tempfile || !$timestamp || !$user_id){
111 die "To archive a tempfile you need to provide: tempfile, subdirectory, archive_filename, timestamp, archive_path, and user_id\n";
114 if (!any
{ $_ eq "curator" || $_ eq "submitter" || $_ eq "sequencer" } ($self->user_role) ) {
115 die "You have insufficient privileges to archive a file.\n". Dumper
$self->user_role;
117 # if (!$subdirectory || !$tempfile || !$archive_filename ) {
118 # print STDERR "File archive failed: incomplete information to archive file.\n";
119 # die "File archive failed: incomplete information to archive file.\n";
121 if ($self->include_timestamp){
122 $file_destination = catfile
($archive_path, $user_id, $subdirectory, $timestamp."_".$archive_filename);
125 $file_destination = catfile
($archive_path, $user_id, $subdirectory, $archive_filename);
127 if ($second_subdirectory) {
128 if ($self->include_timestamp){
129 $file_destination = catfile
($archive_path, $user_id, $subdirectory, $second_subdirectory, $timestamp."_".$archive_filename);
132 $file_destination = catfile
($archive_path, $user_id, $subdirectory, $second_subdirectory, $archive_filename);
134 if ($third_subdirectory) {
135 if ($self->include_timestamp){
136 $file_destination = catfile
($archive_path, $user_id, $subdirectory, $second_subdirectory, $third_subdirectory, $timestamp."_".$archive_filename);
139 $file_destination = catfile
($archive_path, $user_id, $subdirectory, $second_subdirectory, $third_subdirectory, $archive_filename);
145 if (!-d
$archive_path) {
148 if (! -d catfile
($archive_path, $user_id)) {
149 mkdir (catfile
($archive_path, $user_id));
151 if (! -d catfile
($archive_path, $user_id, $subdirectory)) {
152 mkdir (catfile
($archive_path, $user_id, $subdirectory));
154 if ($second_subdirectory) {
155 if (! -d catfile
($archive_path, $user_id, $subdirectory, $second_subdirectory)) {
156 mkdir (catfile
($archive_path, $user_id, $subdirectory, $second_subdirectory));
159 if ($second_subdirectory && $third_subdirectory) {
160 if (! -d catfile
($archive_path, $user_id, $subdirectory, $second_subdirectory, $third_subdirectory)) {
161 mkdir (catfile
($archive_path, $user_id, $subdirectory, $second_subdirectory, $third_subdirectory));
165 copy
($tempfile,$file_destination);
168 $error = "Error saving archived file: $file_destination\n$_";
171 print STDERR
"$error\n";
173 print STDERR
"ARCHIVED: $file_destination\n";
174 return $file_destination;
179 my $file_name_and_location = shift;
180 #print STDERR $file_name_and_location;
182 open(my $F, "<", $file_name_and_location) || die "Can't open file $file_name_and_location";
184 my $md5 = Digest
::MD5
->new();