4 SGN::Controller::AJAX::DocumentBrowser - a REST controller class to provide the backend for uploading documents and searching them using the document browser tool.
13 package SGN
::Controller
::AJAX
::DocumentBrowser
;
19 use File
::Spec
::Functions
;
23 use File
::Basename qw
| basename dirname
|;
26 BEGIN { extends
'Catalyst::Controller::REST' }
29 default => 'application/json',
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) {
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();
48 $c->stash->{rest
} = { error
=> 'Must be logged in!' };
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,
62 user_role
=> $user_type
64 my $archived_filename_with_path = $uploader->archive();
65 if (!$archived_filename_with_path){
66 $c->stash->{rest
} = { error
=> 'Problem archiving the file!' };
69 my $md5 = $uploader->get_md5($archived_filename_with_path);
71 $c->stash->{rest
} = { error
=> 'Problem retrieving file md5!' };
75 my $md_row = $metadata_schema->resultset("MdMetadata")->create({create_person_id
=> $user_id});
77 my $file_row = $metadata_schema->resultset("MdFiles")
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(),
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) {
93 my $file_ids = $c->req->param("file_ids");
94 my $search = $c->req->param("search");
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}});
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;
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};