blib/lib/Yukki/Model/File.pm untested

filter: Statementstmt 0.0% 0 / 153 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 255 CC 23 SCAR 27.8
CC
23
Cov
0%
store
56.0
title
30.0
rename
20.0
CRAP 16.2
1 package Yukki::Model::File;
2 {
3 $Yukki::Model::File::VERSION = '0.140290';
4 }
5 use 5.12.1;
6 use Moose;
7  
8 extends 'Yukki::Model';
9  
10 use Class::Load;
11 use Digest::SHA1 qw( sha1_hex );
12 use Number::Bytes::Human qw( format_bytes );
13 use LWP::MediaTypes qw( guess_media_type );
14 use Path::Class;
15 use Yukki::Error qw( http_throw );
16  
17 # ABSTRACT: the model for loading and saving files in the wiki
18  
19  
20 has path => (
21 is => 'ro',
22 isa => 'Str',
23 required => 1,
24 );
25  
26  
27 has filetype => (
28 is => 'ro',
29 isa => 'Maybe[Str]',
30 required => 1,
31 default => 'yukki',
32 );
33  
34  
35 has repository => (
36 is => 'ro',
37 isa => 'Yukki::Model::Repository',
38 required => 1,
39 handles => {
40 'make_blob' => 'make_blob',
41 'make_blob_from_file' => 'make_blob_from_file',
42 'find_root' => 'find_root',
43 'branch ' => 'branch',
44 'show' => 'show',
45 'make_tree' => 'make_tree',
46 'commit_tree' => 'commit_tree',
47 'update_root' => 'update_root',
48 'find_path' => 'find_path',
49 'fetch_size' => 'fetch_size',
50 'repository_name' => 'name',
51 'author_name' => 'author_name',
52 'author_email' => 'author_email',
53 'log' => 'log',
54 'diff_blobs' => 'diff_blobs',
55 },
56 );
57  
58  
59 sub BUILDARGS {
60 my $class = shift;
61  
62 my %args;
63 if (@_ == 1) { %args = %{ $_[0] }; }
64 else { %args = @_; }
65  
66 if ($args{full_path}) {
67 my $full_path = delete $args{full_path};
68  
69 my ($path, $filetype) = $full_path =~ m{^(.*)(?:\.(\w+))?$};
70  
71 $args{path} = $path;
72 $args{filetype} = $filetype;
73 }
74  
75 return \%args;
76 }
77  
78  
79 sub full_path {
80 my $self = shift;
81  
82 my $full_path;
83 given ($self->filetype) {
84 when (defined) { $full_path = join '.', $self->path, $self->filetype }
85 default { $full_path = $self->path }
86 }
87  
88 return $full_path;
89 }
90  
91  
92 sub file_name {
93 my $self = shift;
94 my $full_path = $self->full_path;
95 my ($file_name) = $full_path =~ m{([^/]+)$};
96 return $file_name;
97 }
98  
99  
100 sub file_id {
101 my $self = shift;
102 return sha1_hex($self->file_name);
103 }
104  
105  
106 sub object_id {
107 my $self = shift;
108 return $self->find_path($self->full_path);
109 }
110  
111  
112 sub title {
113 my $self = shift;
114  
115 if ($self->filetype eq 'yukki') {
116 LINE: for my $line ($self->fetch) {
117 if ($line =~ /^#\s*(.*)$/) {
118 return $1;
119 }
120 elsif ($line =~ /:/) {
121 my ($name, $value) = split m{\s*:\s*}, $line, 2;
122 return $value if lc($name) eq 'title';
123 }
124 else {
125 last LINE;
126 }
127 }
128 }
129  
130 my $title = $self->file_name;
131 $title =~ s/\.(\w+)$//g;
132 return $title;
133 }
134  
135  
136 sub file_size {
137 my $self = shift;
138 return $self->fetch_size($self->full_path);
139 }
140  
141  
142 sub formatted_file_size {
143 my $self = shift;
144 return format_bytes($self->file_size);
145 }
146  
147  
148 sub media_type {
149 my $self = shift;
150 return guess_media_type($self->full_path);
151 }
152  
153  
154 sub store {
155 my ($self, $params) = @_;
156 my $path = $self->full_path;
157  
158 my (@parts) = split m{/}, $path;
159 my $blob_name = $parts[-1];
160  
161 my $object_id;
162 if ($params->{content}) {
163 $object_id = $self->make_blob($blob_name, $params->{content});
164 }
165 elsif ($params->{filename}) {
166 $object_id = $self->make_blob_from_file($blob_name, $params->{filename});
167 }
168 http_throw("unable to create blob for $path") unless $object_id;
169  
170 my $old_tree_id = $self->find_root;
171 http_throw("unable to locate original tree ID for ".$self->branch)
172 unless $old_tree_id;
173  
174 my $new_tree_id = $self->make_tree($old_tree_id, \@parts, $object_id);
175 http_throw("unable to create the new tree containing $path\n")
176 unless $new_tree_id;
177  
178 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
179 http_throw("unable to commit the new tree containing $path\n")
180 unless $commit_id;
181  
182 $self->update_root($old_tree_id, $commit_id);
183 }
184  
185  
186 sub rename {
187 my ($self, $params) = @_;
188 my $old_path = $self->full_path;
189  
190 my (@new_parts) = split m{/}, $params->{full_path};
191 my (@old_parts) = split m{/}, $old_path;
192 my $blob_name = $old_parts[-1];
193  
194 my $object_id = $self->object_id;
195  
196 my $old_tree_id = $self->find_root;
197 http_throw("unable to locate original tree ID for ".$self->branch)
198 unless $old_tree_id;
199  
200 my $new_tree_id = $self->make_tree(
201 $old_tree_id, \@old_parts, \@new_parts, $object_id);
202 http_throw("unable to create the new tree renaming $old_path to $params->{full_path}\n")
203 unless $new_tree_id;
204  
205 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
206 http_throw("unable to commit the new tree renaming $old_path to $params->{full_path}\n")
207 unless $commit_id;
208  
209 $self->update_root($old_tree_id, $commit_id);
210  
211 return Yukki::Model::File->new(
212 app => $self->app,
213 repository => $self->repository,
214 full_path => $params->{full_path},
215 );
216 }
217  
218  
219 sub remove {
220 my ($self, $params) = @_;
221 my $old_path = $self->full_path;
222  
223 my (@old_parts) = split m{/}, $old_path;
224  
225 my $old_tree_id = $self->find_root;
226 http_throw("unable to locate original tree ID for ".$self->branch)
227 unless $old_tree_id;
228  
229 my $new_tree_id = $self->make_tree($old_tree_id, \@old_parts);
230 http_throw("unable to create the new tree removing $old_path\n")
231 unless $new_tree_id;
232  
233 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
234 http_throw("unable to commit the new tree removing $old_path\n")
235 unless $commit_id;
236  
237 $self->update_root($old_tree_id, $commit_id);
238 }
239  
240  
241 sub exists {
242 my $self = shift;
243  
244 my $path = $self->full_path;
245 return $self->find_path($path);
246 }
247  
248  
249 sub fetch {
250 my $self = shift;
251  
252 my $path = $self->full_path;
253 my $object_id = $self->find_path($path);
254  
255 return unless defined $object_id;
256  
257 return $self->show($object_id);
258 }
259  
260  
261 sub has_format {
262 my ($self, $media_type) = @_;
263 $media_type //= $self->media_type;
264  
265 my @formatters = $self->app->formatter_plugins;
266 for my $formatter (@formatters) {
267 return 1 if $formatter->has_format($media_type);
268 }
269  
270 return '';
271 }
272  
273  
274 sub fetch_formatted {
275 my ($self, $ctx, $position) = @_;
276 $position //= 0;
277  
278 my $media_type = $self->media_type;
279  
280 my $formatter;
281 for my $plugin ($self->app->formatter_plugins) {
282 return $plugin->format({
283 context => $ctx,
284 file => $self,
285 position => $position,
286 }) if $plugin->has_format($media_type);
287 }
288  
289 return $self->fetch;
290 }
291  
292  
293 sub history {
294 my $self = shift;
295 return $self->log($self->full_path);
296 }
297  
298  
299 sub diff {
300 my ($self, $object_id_1, $object_id_2) = @_;
301 return $self->diff_blobs($self->full_path, $object_id_1, $object_id_2);
302 }
303  
304  
305 sub file_preview {
306 my ($self, %params) = @_;
307  
308 Class::Load::load_class('Yukki::Model::FilePreview');
309 return Yukki::Model::FilePreview->new(
310 %params,
311 app => $self->app,
312 repository => $self->repository,
313 path => $self->path,
314 );
315 }
316  
317  
318 sub list_files {
319 my ($self) = @_;
320 return $self->repository->list_files($self->path);
321 }
322  
323  
324 sub parent {
325 my $self = shift;
326  
327 my @parts = split m{/}, $self->path;
328 return if @parts == 1;
329  
330 pop @parts;
331 return Yukki::Model::File->new(
332 app => $self->app,
333 repository => $self->repository,
334 path => join('/', @parts),
335 );
336 }
337  
338 1;
339  
340 __END__