Merge pull request #3855 from solgenomics/topic/upgraded_fieldmap
[sgn.git] / lib / CXGN / UploadFile.pm
blob885ef01a44c4ac75b9701bffc4474b3df7ad5908
1 package CXGN::UploadFile;
3 =head1 NAME
5 CXGN::UploadFile - an object to handle uploading files
7 =head1 USAGE
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',
16 user_id => 41,
17 user_role => 'curator'
18 });
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
24 =head1 DESCRIPTION
27 =head1 AUTHORS
29 Jeremy D. Edwards (jde22@cornell.edu)
31 =cut
33 use strict;
34 use warnings;
35 use Moose;
36 use Try::Tiny;
37 use List::MoreUtils qw /any /;
38 use File::Copy;
39 use File::Spec::Functions;
40 use File::Basename qw | basename dirname|;
41 use Digest::MD5;
42 use Data::Dumper;
44 has 'tempfile' => (isa => "Str",
45 is => 'rw',
46 required => 0
49 has 'subdirectory' => (isa => "Str",
50 is => 'rw',
51 required => 0
54 has 'second_subdirectory' => (isa => "Str",
55 is => 'rw',
56 required => 0
59 has 'third_subdirectory' => (isa => "Str",
60 is => 'rw',
61 required => 0
64 has 'archive_path' => (isa => "Str",
65 is => 'rw',
66 required => 0
69 has 'archive_filename' => (isa => "Str",
70 is => 'rw',
71 required => 0
74 has 'timestamp' => (isa => "Str",
75 is => 'rw',
76 required => 0
79 has 'user_id' => (isa => "Int",
80 is => 'rw',
81 required => 0
84 has 'user_role' => (isa => "Str",
85 is => 'rw',
86 required => 0
89 has 'include_timestamp' => (
90 isa => "Bool",
91 is => 'rw',
92 required => 0,
93 default => 1
96 sub archive {
97 my $self = shift;
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;
107 my $error;
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);
124 else {
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);
131 else {
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);
138 else {
139 $file_destination = catfile($archive_path, $user_id, $subdirectory, $second_subdirectory, $third_subdirectory, $archive_filename);
144 try {
145 if (!-d $archive_path) {
146 mkdir $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);
167 catch {
168 $error = "Error saving archived file: $file_destination\n$_";
170 if ($error) {
171 print STDERR "$error\n";
173 print STDERR "ARCHIVED: $file_destination\n";
174 return $file_destination;
177 sub get_md5 {
178 my $self = shift;
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";
183 binmode $F;
184 my $md5 = Digest::MD5->new();
185 $md5->addfile($F);
186 close($F);
187 return $md5;