| 1 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package Smartcat::App::DocumentExportApi; | ||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use File::Spec::Functions qw(catfile); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use IO::Uncompress::Unzip qw($UnzipError); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Log::Any qw($log); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Smartcat::Client::DocumentExportApi; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use Smartcat::App::Constants qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | EXPORT_ZIP_FILES_COUNT | ||||||||||
| 14 | MAX_ITERATION_WAIT_TIMEOUT | ||||||||||
| 15 | ITERATION_WAIT_TIMEOUT | ||||||||||
| 16 | 2 | ); | |||||||||
| 17 | 2 | ▶ | use Smartcat::App::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | $Carp::Internal{ ('Smartcat::Client::DocumentExportApi') }++; | |||||||||
| 21 | 2 | $Carp::Internal{ (__PACKAGE__) }++; | |||||||||
| 22 | |||||||||||
| 23 | 2 | ▶ | use Log::Any qw($log); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 28 | 1 | my ( $class, $api, $rundata ) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 1 | my $self = bless( | |||||||||
| 31 | { | ||||||||||
| 32 | api => Smartcat::Client::DocumentExportApi->new($api), | ||||||||||
| 33 | rundata => $rundata | ||||||||||
| 34 | }, | ||||||||||
| 35 | $class | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 1 | return $self; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub export_files { | ||||||||
|
Subroutine
export_files: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||
| 42 | 0 | my ( $self, $documents ) = @_; | |||||||||
| 43 | |||||||||||
| 44 | 0 | my $api = $self->{api}; | |||||||||
| 45 | 0 | my $rundata = $self->{rundata}; | |||||||||
| 46 | 0 | my %docs = map { $_->id => $_ } @$documents; | |||||||||
| 47 | 0 | my @doc_ids = keys %docs; | |||||||||
| 48 | 0 | my @tasks; | |||||||||
| 49 | 0 | ▶ | while ( $#doc_ids >= 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | my @task_doc_ids = splice( @doc_ids, 0, EXPORT_ZIP_FILES_COUNT ); | |||||||||
| 51 | 0 | my $task = eval { | |||||||||
| 52 | $api->document_export_request_export( | ||||||||||
| 53 | document_ids => \@task_doc_ids, | ||||||||||
| 54 | 0 | mode => $rundata->{mode} ); | |||||||||
| 55 | }; | ||||||||||
| 56 | 0 | ▶ | die $log->error( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | sprintf( | ||||||||||
| 58 | "Failed to register a task for exporting documents: '%s'.\nError:\n%s", | ||||||||||
| 59 | join( ', ', @task_doc_ids ), | ||||||||||
| 60 | format_error_message($@) | ||||||||||
| 61 | ) | ||||||||||
| 62 | ) unless ($task); | ||||||||||
| 63 | 0 | push @tasks, $task; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 0 | foreach my $task (@tasks) { | |||||||||
| 67 | 0 | my $single_file_export = $#{ $task->document_ids } == 0; | |||||||||
| 68 | 0 | my $response; | |||||||||
| 69 | 0 | my $counter = 0; | |||||||||
| 70 | |||||||||||
| 71 | #die $task->id; | ||||||||||
| 72 | 0 | $log->info( sprintf( "Processing task '%s'...", $task->id ) ); | |||||||||
| 73 | 0 | while ( 1 ) { | |||||||||
| 74 | 0 | $log->info("Downloading exported files..."); | |||||||||
| 75 | 0 | $response = eval { | |||||||||
| 76 | 0 | $api->document_export_download_export_result( | |||||||||
| 77 | task_id => $task->id ); | ||||||||||
| 78 | }; | ||||||||||
| 79 | 0 | ▶ | unless ($response) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | die $log->error( | |||||||||
| 81 | sprintf( | ||||||||||
| 82 | "Failed to download exported files for task '%s'.\nError:\n%s", | ||||||||||
| 83 | $task->id, format_error_message($@) | ||||||||||
| 84 | ) | ||||||||||
| 85 | ); | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | last if $response->code != 204; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | $log->info("Export is not ready..."); | |||||||||
| 90 | 0 | $counter++; | |||||||||
| 91 | 0 | my $timeout = ITERATION_WAIT_TIMEOUT * $counter; | |||||||||
| 92 | 0 | ▶ | sleep($timeout < MAX_ITERATION_WAIT_TIMEOUT ? $timeout : MAX_ITERATION_WAIT_TIMEOUT); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | } | ||||||||||
| 94 | die $log->error( | ||||||||||
| 95 | sprintf( "Cannot download exported files: %s. Export task is failed.", | ||||||||||
| 96 | 0 | ▶ | join( ', ', @{ $task->document_ids } ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | ) if $response->code == 422; | ||||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | if ($single_file_export) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | my $doc = $docs{ @{ $task->document_ids }[0] }; | |||||||||
| 101 | 0 | my $name = $doc->full_path; | |||||||||
| 102 | 0 | $log->info("Processing document '$name'..."); | |||||||||
| 103 | my $filepath = get_file_path( $rundata->{project_workdir}, | ||||||||||
| 104 | 0 | $doc->target_language, $name, $rundata->{filetype} ); | |||||||||
| 105 | 0 | save_file( $filepath, $response->content ); | |||||||||
| 106 | 0 | $log->info("Saved to '$filepath'."); | |||||||||
| 107 | } | ||||||||||
| 108 | else { | ||||||||||
| 109 | 0 | $self->_save_exported_files_from_zip( $task, $response->content ); | |||||||||
| 110 | } | ||||||||||
| 111 | } | ||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | sub _save_exported_files_from_zip { | ||||||||
|
Subroutine
_save_exported_files_from_zip: not called CC=8 SCAR=42.8
| |||||||||||
| 115 | 0 | my ( $self, $task, $content ) = @_; | |||||||||
| 116 | |||||||||||
| 117 | 0 | ▶ | my $u = new IO::Uncompress::Unzip \$content | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | or die $log->error( | ||||||||||
| 119 | "Cannot open downloaded content of $task->id: $UnzipError"); | ||||||||||
| 120 | 0 | my $rundata = $self->{rundata}; | |||||||||
| 121 | 0 | my $status = 0; | |||||||||
| 122 | 0 | $log->info("Processing zipped exported file..."); | |||||||||
| 123 | 0 | ▶ | do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | my $name = $u->getHeaderInfo()->{Name}; | |||||||||
| 125 | 0 | ▶ | if ($name =~ m/$rundata->{filetype}$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | ▶ | die $log->error( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | "Cannot parse '$name' (filetype='$rundata->{filetype}') to get filename and target_language" | ||||||||||
| 128 | ) unless $name =~ m/(.*)\((.*)\)$rundata->{filetype}$/; | ||||||||||
| 129 | 0 | $log->info("Processing member '$name'..."); | |||||||||
| 130 | 0 | my $target_language = $2; | |||||||||
| 131 | my $filepath = | ||||||||||
| 132 | 0 | get_file_path($rundata->{project_workdir}, $target_language, $1, $rundata->{filetype}); | |||||||||
| 133 | #print Dumper $self; | ||||||||||
| 134 | 0 | ▶ | open( my $fh, '>', $filepath ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | or die $log->error("Could not open file '$filepath' $!"); | ||||||||||
| 136 | 0 | binmode($fh); | |||||||||
| 137 | 0 | ▶ | print $fh $_ while <$u>; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 0 | close $fh; | |||||||||
| 139 | 0 | $log->info("Saved to '$filepath'."); | |||||||||
| 140 | } else { | ||||||||||
| 141 | 0 | $log->info("Skipping member '$name'..."); | |||||||||
| 142 | } | ||||||||||
| 143 | } while ( ( $status = $u->nextStream() ) > 0 ); | ||||||||||
| 144 | 0 | ▶ | die $log->error("Error processing downloaded content of $task->id: $!") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | if $status < 0; | ||||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 2 | 1; | |||||||||