ignore emacs backup files also in db/run_all_patches.pl
[sgn.git] / lib / CXGN / UploadFile.pm
blob823ca430d3981b3fff2114e18ebee6367a73a737
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 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);
110 else {
111 $file_destination = catfile($archive_path, $user_id, $subdirectory,$archive_filename);
113 try {
114 if (!-d $archive_path) {
115 mkdir $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);
124 } catch {
125 $error = "Error saving archived file: $file_destination\n$_";
127 if ($error) {
128 die "$error\n";
130 print STDERR "ARCHIVED: $file_destination\n";
131 return $file_destination;
134 sub get_md5 {
135 my $self = shift;
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 ";
140 binmode $F;
141 my $md5 = Digest::MD5->new();
142 $md5->addfile($F);
143 close($F);
144 return $md5;