seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / DocumentBrowser.pm
bloba946ea4928a54ed50baa549d21c8da90b80bc85f
2 =head1 NAME
4 SGN::Controller::AJAX::DocumentBrowser - a REST controller class to provide the backend for uploading documents and searching them using the document browser tool.
6 =head1 DESCRIPTION
9 =head1 AUTHOR
11 =cut
13 package SGN::Controller::AJAX::DocumentBrowser;
15 use Moose;
16 use Try::Tiny;
17 use DateTime;
18 use File::Slurp;
19 use File::Spec::Functions;
20 use File::Copy;
21 use Data::Dumper;
22 use CXGN::UploadFile;
23 use File::Basename qw | basename dirname|;
24 use JSON;
26 BEGIN { extends 'Catalyst::Controller::REST' }
28 __PACKAGE__->config(
29 default => 'application/json',
30 stash_key => 'rest',
31 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
35 sub upload_document : Path('/ajax/tools/documents/upload') : ActionClass('REST') { }
36 sub upload_document_POST : Args(0) {
37 my ($self, $c) = @_;
38 my $schema = $c->dbic_schema("Bio::Chado::Schema");
39 my $metadata_schema = $c->dbic_schema("CXGN::Metadata::Schema");
41 my $upload = $c->req->upload('upload_document_browser_file_input');
42 my $upload_original_name = $upload->filename();
43 my $upload_tempfile = $upload->tempname;
44 my $time = DateTime->now();
45 my $timestamp = $time->ymd()."_".$time->hms();
47 if (!$c->user){
48 $c->stash->{rest} = { error => 'Must be logged in!' };
49 $c->detach;
52 my $user_id = $c->user->get_object->get_sp_person_id;
53 my $user_type = $c->user->get_object->get_user_type();
55 my $uploader = CXGN::UploadFile->new({
56 tempfile => $upload_tempfile,
57 subdirectory => 'document_browser',
58 archive_path => $c->config->{archive_path},
59 archive_filename => $upload_original_name,
60 timestamp => $timestamp,
61 user_id => $user_id,
62 user_role => $user_type
63 });
64 my $archived_filename_with_path = $uploader->archive();
65 if (!$archived_filename_with_path){
66 $c->stash->{rest} = { error => 'Problem archiving the file!' };
67 $c->detach;
69 my $md5 = $uploader->get_md5($archived_filename_with_path);
70 if (!$md5){
71 $c->stash->{rest} = { error => 'Problem retrieving file md5!' };
72 $c->detach;
75 my $md_row = $metadata_schema->resultset("MdMetadata")->create({create_person_id => $user_id});
76 $md_row->insert();
77 my $file_row = $metadata_schema->resultset("MdFiles")
78 ->create({
79 basename => basename($archived_filename_with_path),
80 dirname => dirname($archived_filename_with_path),
81 filetype => 'document_browser',
82 md5checksum => $md5->hexdigest(),
83 metadata_id => $md_row->metadata_id(),
84 });
85 $file_row->insert();
87 $c->stash->{rest} = {success => 'Successfully saved file!'};
90 sub search_document : Path('/ajax/tools/documents/search') : ActionClass('REST') { }
91 sub search_document_POST : Args(0) {
92 my ($self, $c) = @_;
93 my $file_ids = $c->req->param("file_ids");
94 my $search = $c->req->param("search");
96 my $file_ids_ref;
97 if ($file_ids){
98 $file_ids_ref = decode_json $file_ids;
101 my $metadata_schema = $c->dbic_schema("CXGN::Metadata::Schema");
102 my $file_rs = $metadata_schema->resultset("MdFiles")->search({file_id => {-in => $file_ids_ref}});
103 my @files;
104 while(my $r = $file_rs->next()){
105 my $dirname = $r->dirname();
106 my $basename = $r->basename();
107 my $file_path = $dirname. "/" .$basename;
108 push @files, $file_path;
111 my @found_lines;
112 my @list_elements;
113 foreach my $f (@files){
114 open(my $fh, '<', $f)
115 or die "Could not open file '$f' $!";
117 while (my $row = <$fh>) {
118 if (index($row, $search) != -1) {
119 push @found_lines, $row;
120 my @col = split "\t", $row;
121 push @list_elements, $col[0];
125 $c->stash->{rest} = {success => 1, found_lines => \@found_lines, list_elements => \@list_elements};
128 #########
130 #########