source: main/trunk/greenstone2/perllib/cpan/Mojo/Asset/Memory.pm@ 32205

Last change on this file since 32205 was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 3.7 KB
Line 
1package Mojo::Asset::Memory;
2use Mojo::Base 'Mojo::Asset';
3
4use Mojo::Asset::File;
5use Mojo::File 'path';
6
7has 'auto_upgrade';
8has max_memory_size => sub { $ENV{MOJO_MAX_MEMORY_SIZE} || 262144 };
9has mtime => sub {$^T};
10
11sub add_chunk {
12 my ($self, $chunk) = @_;
13
14 # Upgrade if necessary
15 $self->{content} .= $chunk;
16 return $self if !$self->auto_upgrade || $self->size <= $self->max_memory_size;
17 $self->emit(upgrade => my $file = $self->to_file);
18 return $file;
19}
20
21sub contains {
22 my ($self, $str) = @_;
23
24 my $start = $self->start_range;
25 my $pos = index $self->{content} // '', $str, $start;
26 $pos -= $start if $start && $pos >= 0;
27 my $end = $self->end_range;
28
29 return $end && ($pos + length $str) >= $end ? -1 : $pos;
30}
31
32sub get_chunk {
33 my ($self, $offset, $max) = @_;
34 $max //= 131072;
35
36 $offset += $self->start_range;
37 if (my $end = $self->end_range) {
38 $max = $end + 1 - $offset if ($offset + $max) > $end;
39 }
40
41 return substr shift->{content} // '', $offset, $max;
42}
43
44sub move_to { path($_[1])->spurt($_[0]{content} // '') and return $_[0] }
45
46sub size { length(shift->{content} // '') }
47
48sub slurp { shift->{content} // '' }
49
50sub to_file { Mojo::Asset::File->new->add_chunk(shift->slurp) }
51
521;
53
54=encoding utf8
55
56=head1 NAME
57
58Mojo::Asset::Memory - In-memory storage for HTTP content
59
60=head1 SYNOPSIS
61
62 use Mojo::Asset::Memory;
63
64 my $mem = Mojo::Asset::Memory->new;
65 $mem->add_chunk('foo bar baz');
66 say $mem->slurp;
67
68=head1 DESCRIPTION
69
70L<Mojo::Asset::Memory> is an in-memory storage backend for HTTP content.
71
72=head1 EVENTS
73
74L<Mojo::Asset::Memory> inherits all events from L<Mojo::Asset> and can emit the
75following new ones.
76
77=head2 upgrade
78
79 $mem->on(upgrade => sub {
80 my ($mem, $file) = @_;
81 ...
82 });
83
84Emitted when asset gets upgraded to a L<Mojo::Asset::File> object.
85
86 $mem->on(upgrade => sub {
87 my ($mem, $file) = @_;
88 $file->tmpdir('/tmp');
89 });
90
91=head1 ATTRIBUTES
92
93L<Mojo::Asset::Memory> inherits all attributes from L<Mojo::Asset> and
94implements the following new ones.
95
96=head2 auto_upgrade
97
98 my $bool = $mem->auto_upgrade;
99 $mem = $mem->auto_upgrade($bool);
100
101Try to detect if content size exceeds L</"max_memory_size"> limit and
102automatically upgrade to a L<Mojo::Asset::File> object.
103
104=head2 max_memory_size
105
106 my $size = $mem->max_memory_size;
107 $mem = $mem->max_memory_size(1024);
108
109Maximum size in bytes of data to keep in memory before automatically upgrading
110to a L<Mojo::Asset::File> object, defaults to the value of the
111C<MOJO_MAX_MEMORY_SIZE> environment variable or C<262144> (256KiB).
112
113=head2 mtime
114
115 my $mtime = $mem->mtime;
116 $mem = $mem->mtime(1408567500);
117
118Modification time of asset, defaults to the value of C<$^T>.
119
120=head1 METHODS
121
122L<Mojo::Asset::Memory> inherits all methods from L<Mojo::Asset> and implements
123the following new ones.
124
125=head2 add_chunk
126
127 $mem = $mem->add_chunk('foo bar baz');
128 my $file = $mem->add_chunk('abc' x 262144);
129
130Add chunk of data and upgrade to L<Mojo::Asset::File> object if necessary.
131
132=head2 contains
133
134 my $position = $mem->contains('bar');
135
136Check if asset contains a specific string.
137
138=head2 get_chunk
139
140 my $bytes = $mem->get_chunk($offset);
141 my $bytes = $mem->get_chunk($offset, $max);
142
143Get chunk of data starting from a specific position, defaults to a maximum
144chunk size of C<131072> bytes (128KiB).
145
146=head2 move_to
147
148 $mem = $mem->move_to('/home/sri/foo.txt');
149
150Move asset data into a specific file.
151
152=head2 size
153
154 my $size = $mem->size;
155
156Size of asset data in bytes.
157
158=head2 slurp
159
160 my $bytes = mem->slurp;
161
162Read all asset data at once.
163
164=head2 to_file
165
166 my $file = $mem->to_file;
167
168Convert asset to L<Mojo::Asset::File> object.
169
170=head1 SEE ALSO
171
172L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
173
174=cut
Note: See TracBrowser for help on using the repository browser.