add trial design store subclasses.
[sgn.git] / lib / CXGN / UploadFile.pm
blob8f2515a77617bf72164959af6ad710544a9f294e
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 archive_path => '/some/path/to/dir',
13 archive_filename => 'myfilename.csv',
14 timestamp => '2016-09-24_10:30:30',
15 user_id => 41,
16 user_role => 'curator'
17 });
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
23 =head1 DESCRIPTION
26 =head1 AUTHORS
28 Jeremy D. Edwards (jde22@cornell.edu)
30 =cut
32 use strict;
33 use warnings;
34 use Moose;
35 use Try::Tiny;
36 use List::MoreUtils qw /any /;
37 use File::Copy;
38 use File::Spec::Functions;
39 use File::Basename qw | basename dirname|;
40 use Digest::MD5;
41 use Data::Dumper;
43 has 'tempfile' => (isa => "Str",
44 is => 'rw',
45 required => 0
48 has 'subdirectory' => (isa => "Str",
49 is => 'rw',
50 required => 0
53 has 'archive_path' => (isa => "Str",
54 is => 'rw',
55 required => 0
58 has 'archive_filename' => (isa => "Str",
59 is => 'rw',
60 required => 0
63 has 'timestamp' => (isa => "Str",
64 is => 'rw',
65 required => 0
68 has 'user_id' => (isa => "Int",
69 is => 'rw',
70 required => 0
73 has 'user_role' => (isa => "Str",
74 is => 'rw',
75 required => 0
78 has 'include_timestamp' => (
79 isa => "Bool",
80 is => 'rw',
81 required => 0,
82 default => 1
85 sub archive {
86 my $self = shift;
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;
93 my $file_destination;
94 my $error;
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);
111 else {
112 $file_destination = catfile($archive_path, $user_id, $subdirectory,$archive_filename);
114 try {
115 if (!-d $archive_path) {
116 mkdir $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);
125 } catch {
126 $error = "Error saving archived file: $file_destination\n$_";
128 if ($error) {
129 print STDERR "$error\n";
131 print STDERR "ARCHIVED: $file_destination\n";
132 return $file_destination;
135 sub get_md5 {
136 my $self = shift;
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";
141 binmode $F;
142 my $md5 = Digest::MD5->new();
143 $md5->addfile($F);
144 close($F);
145 return $md5;