|
| 1 | +# Copyright 2006-2011 by Swami Dhyan Nataraj (Nikolay Shaplov) |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions# are met: |
| 5 | +# 1. Redistributions of source code must retain the above copyright |
| 6 | +# notice, this list of conditions and the following disclaimer. |
| 7 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer in the |
| 9 | +# documentation and/or other materials provided with the distribution. |
| 10 | +# 3. The name of the author may not be used to endorse or promote products |
| 11 | +# derived from this software without specific prior written permission. |
| 12 | +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 13 | +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 14 | +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 15 | +# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 16 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 17 | +# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 18 | +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 19 | +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 20 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 21 | + |
| 22 | +package fb2::Images; |
| 23 | +#manipulate embedded images in the FictionBook file |
| 24 | + |
| 25 | +use XML::LibXML; |
| 26 | +use File::MMagic; |
| 27 | +use MIME::Base64 qw(encode_base64); |
| 28 | +use Encode; |
| 29 | +our $VERSION=0.02.2; |
| 30 | + |
| 31 | +sub printImageList |
| 32 | +{ |
| 33 | + my $List=shift; |
| 34 | + foreach (@{$List}) |
| 35 | + { |
| 36 | + print "$_\n"; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +sub getImageList |
| 41 | +{ |
| 42 | + my $doc=shift; |
| 43 | + my @list=(); |
| 44 | + |
| 45 | + foreach ($doc->getDocumentElement()->getElementsByTagName('binary' ,0) ) |
| 46 | + { |
| 47 | + my $id=$_->getAttribute('id'); |
| 48 | + push @list,$id if ($id) |
| 49 | + } |
| 50 | + return \@list |
| 51 | +} |
| 52 | + |
| 53 | +sub getUsedIdList |
| 54 | +{ |
| 55 | + my $doc=shift; |
| 56 | + my @list=(); |
| 57 | + |
| 58 | + foreach ($doc->getDocumentElement()->getElementsByTagName('*' ,1) ) |
| 59 | + { |
| 60 | + my $id=$_->getAttribute('id'); |
| 61 | + push @list,$id if $id |
| 62 | + } |
| 63 | + return \@list |
| 64 | +} |
| 65 | + |
| 66 | +sub AddImages |
| 67 | +{ |
| 68 | + my $doc = shift; |
| 69 | + my $opts = shift; |
| 70 | + my $flags = shift; |
| 71 | + |
| 72 | + foreach (@{$opts->{'add'}}) |
| 73 | + { |
| 74 | + my $flag=1; |
| 75 | + my $ImageName=$_; |
| 76 | + print "Adding image $_ ...\n"; |
| 77 | + foreach (@{getImageList($doc)}) |
| 78 | + { |
| 79 | + if ($_ eq $ImageName) |
| 80 | + { |
| 81 | + print STDERR "Image $ImageName already exist\n"; |
| 82 | + $flag=0; |
| 83 | + last; |
| 84 | + } |
| 85 | + } |
| 86 | + if ($flag) |
| 87 | + { |
| 88 | + foreach (@{getUsedIdList($doc)}) |
| 89 | + { |
| 90 | + if ($_ eq $ImageName) |
| 91 | + { |
| 92 | + print STDERR "Object $ImageName already exist\n"; |
| 93 | + $flag=0; |
| 94 | + last; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + if ($flag) |
| 99 | + { |
| 100 | + my $mm= new File::MMagic; |
| 101 | + my $MimeType= $mm->checktype_filename($ImageName); |
| 102 | + open(FILE, $ImageName) or die "$!"; |
| 103 | + local($/) = undef; |
| 104 | + my $Encoded= encode_base64(<FILE>); |
| 105 | + close (FILE); |
| 106 | + my $NewNode = $doc->createElement('binary'); |
| 107 | + $NewNode->setAttribute ('id', $ImageName); |
| 108 | + $NewNode->setAttribute ('content-type',$MimeType); |
| 109 | + $NewNode->appendChild($doc->createTextNode("\n".$Encoded)); |
| 110 | + $doc->getDocumentElement()->appendChild($NewNode); |
| 111 | + $doc->getDocumentElement()->appendChild($doc->createTextNode("\n")); |
| 112 | + $flags->{'changed'}=1; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +sub RemoveImages |
| 118 | +{ |
| 119 | + my $doc = shift; |
| 120 | + my $opts = shift; |
| 121 | + my $flags = shift; |
| 122 | + |
| 123 | + my $root=$doc->getDocumentElement(); |
| 124 | + foreach (@{$opts->{'remove'}}) |
| 125 | + { |
| 126 | + my $ImageName=$_; |
| 127 | + print "Removing image '$_'... "; |
| 128 | + my $flag=1; |
| 129 | + foreach my $binary ($root->getElementsByTagName('binary' ,0)) |
| 130 | + { |
| 131 | + if ($binary->getAttribute('id') eq $ImageName) |
| 132 | + { |
| 133 | + while (1) |
| 134 | + { |
| 135 | + my $prev = $binary->getPreviousSibling(); |
| 136 | + last until defined($prev); |
| 137 | + last until ($prev->nodeType() == XML_TEXT_NODE && $prev->getData() =~ /^\s*$/ ); |
| 138 | + $root->removeChild($prev); |
| 139 | + } |
| 140 | + $root->removeChild($binary); |
| 141 | + print "Done\n"; |
| 142 | + $flag=0; |
| 143 | + $flags->{'changed'}=1; |
| 144 | + } |
| 145 | + } |
| 146 | + print "Not Found!\n" if $flag; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +sub getText |
| 151 | +{ |
| 152 | + my ($elem) = @_; |
| 153 | + my $text = ''; |
| 154 | + for my $node ($elem->getChildNodes()) |
| 155 | + { |
| 156 | + if ($node->nodeType() == XML_ELEMENT_NODE) |
| 157 | + { |
| 158 | + $text .= getText($node); |
| 159 | + } |
| 160 | + elsif ($node->nodeType() == XML_TEXT_NODE) |
| 161 | + { |
| 162 | + $text .= $node->getData(); |
| 163 | + } |
| 164 | + } |
| 165 | + return $text; |
| 166 | +} |
| 167 | + |
| 168 | +sub ExtractImages |
| 169 | +{ |
| 170 | + my ($doc,$opts,$flags) = @_; |
| 171 | + |
| 172 | + my $root=$doc->getDocumentElement(); |
| 173 | + foreach my $ImageName (@{$opts->{'extract'}}) |
| 174 | + { |
| 175 | + print "Extracting image '$ImageName'... "; |
| 176 | + my $flag=1; |
| 177 | + foreach my $binary ($root->getElementsByTagName('binary' ,0)) |
| 178 | + { |
| 179 | + if ($binary->getAttribute('id') eq $ImageName) |
| 180 | + { |
| 181 | + my $data = MIME::Base64::decode_base64( getText( $binary ) ); |
| 182 | + open(FILE, '>', $ImageName) or die "$!"; |
| 183 | + print FILE $data; |
| 184 | + close (FILE); |
| 185 | + print "Done\n"; |
| 186 | + $flag=0; |
| 187 | + last; |
| 188 | + } |
| 189 | + } |
| 190 | + print "Not Found!\n" if $flag; |
| 191 | + } |
| 192 | +} |
0 commit comments