Document/WebGUI/Asset.pm

filter: Statementstmt 84.6% 11 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 88.8% 16 / 18 CC 1 SCAR 0.6
CC
1
Cov
85%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Plugin/WebGUI/Assets.pm ↑ Summary Plugin/WebGUI.pm »
1 package Padre::Document::WebGUI::Asset;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $Padre::Document::WebGUI::Asset::VERSION = '1.002';
4 }
5  
6 # ABSTRACT: Padre::Document subclass representing a WebGUI Asset
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use Padre::Logger;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use Padre::Document ();
14  
15 our @ISA = 'Padre::Document';
16  
17 use Class::XSAccessor getters => {
18 asset => 'asset',
19 url => 'url',
20 };
21  
22  
23 sub basename { $_[0]->filename }
24  
25  
26 sub dirname { $_[0]->basename }
27  
28  
29 sub time_on_file { $_[0]->asset->{revisionDate} }
30  
31  
32 sub load_file { $_[0]->load_asset }
33  
34  
35 sub is_new { 0 }
36  
37  
38 sub lexer { Padre::MimeTypes->get_lexer('text/html') }
39  
40  
41 sub load_asset {
42 my ( $self, $assetId, $url ) = @_;
43  
44 TRACE( "Loading asset $assetId from $url with mimetype " . $self->get_mimetype ) if DEBUG;
45  
46 # TODO: Investigate whether we should actually subclass Padre::File rather than faking it
47 # $self->{file} = {};
48  
49 $self->{url} = $url;
50  
51 # Create a user agent object
52 use LWP::UserAgent;
53 my $ua = LWP::UserAgent->new;
54 my $get = "$url?op=padre&func=edit&assetId=$assetId";
55 TRACE("GET: $get") if DEBUG;
56 my $response = $ua->get($get);
57 unless ( $response->header('Padre-Plugin-WebGUI') ) {
58 my $error = "The server does not appear to have the Padre::Plugin::WebGUI content handler installed";
59 $self->set_errstr($error);
60 $self->editor->main->error($error);
61 return;
62 }
63 if ( !$response->is_success ) {
64 my $error = "The server said:\n" . $response->status_line;
65 $self->set_errstr($error);
66 $self->editor->main->error($error);
67 return;
68 }
69  
70 return $self->process_response( $response->content );
71 }
72  
73  
74 sub save_file {
75 my $self = shift;
76  
77 my $asset = $self->asset;
78 return unless $asset;
79 my $url = $self->url;
80  
81 # Two saves in the same second will cause asset->addRevision to explode
82 return 1 if $self->timestamp && $self->timestamp == time;
83  
84 TRACE("Saving asset $asset->{assetId}") if DEBUG;
85  
86 # Put editor text back into asset hash
87 $self->set_asset_content;
88  
89 # Create a user agent object
90 use LWP::UserAgent;
91 my $ua = LWP::UserAgent->new;
92 my $response = $ua->post(
93 $url,
94 {
95 op => 'padre',
96 func => 'save',
97 assetId => $asset->{assetId},
98 props => encode_json($asset),
99 }
100 );
101 unless ( $response->header('Padre-Plugin-WebGUI') ) {
102 my $error = "The server does not appear to have the Padre::Plugin::WebGUI content handler installed";
103 $self->set_errstr($error);
104 $self->editor->main->error($error);
105 return;
106 }
107 if ( !$response->is_success ) {
108 $self->set_errstr( "The server said:\n" . $response->status_line );
109 return;
110 }
111  
112 return $self->process_response( $response->content );
113 }
114  
115  
116 sub process_response {
117 my $self = shift;
118 my $content = shift;
119  
120 # TRACE($content) if DEBUG;
121  
122 use JSON;
123 my $asset = eval { decode_json($content) };
124 if ($@) {
125 TRACE($@) if DEBUG;
126 warn $@;
127 my $error = "The server sent an invalid response, please try again (and check the logs)";
128 $self->set_errstr($error);
129 $self->editor->main->error($error);
130 return;
131 }
132  
133 $self->{asset} = $asset;
134 $self->{_timestamp} = $self->time_on_file;
135  
136 # Set a fake filename, so that we the file isn't considered 'new'
137 #$self->{filename} = "[$asset->{name}] $asset->{menuTitle}"; # asset name not needed now that icon shown
138 $self->{filename} = $asset->{menuTitle};
139  
140 $self->render;
141  
142 return 1;
143 }
144  
145  
146 sub get_asset_content {
147 my $self = shift;
148 return $self->asset->{content};
149 }
150  
151  
152 sub set_asset_content {
153 my $self = shift;
154 $self->asset->{content} = $self->text_get;
155 }
156  
157  
158 sub render {
159 my $self = shift;
160 my $asset = $self->asset;
161  
162 # Set text (a la Padre::Document::load_file)
163 my $text = $self->get_asset_content || q{};
164  
165 require Padre::Locale;
166 require utf8;
167 $self->{encoding} = Padre::Locale::encoding_from_string($text);
168 if ( not utf8::is_utf8( $text ) ) {
169 my $decoded = eval { Encode::decode( $self->{encoding}, $text ) };
170 if ( $@ ) {
171 my $error = "Error decoding server response.";
172 $self->set_errstr( $error );
173 $self->editor->main->error( $error );
174 }
175 else {
176 $text = $decoded;
177 }
178 }
179 $self->text_set($text);
180 $self->{original_content} = $self->text_get;
181 $self->colourize;
182 }
183  
184  
185 1;
186  
187 __END__
« Plugin/WebGUI/Assets.pm ↑ Summary Plugin/WebGUI.pm »