[MKDoc-commit] [1.6] Added file missing from earlier commit
chris at mkdoc.demon.co.uk
chris at mkdoc.demon.co.uk
Tue Sep 27 16:12:29 BST 2005
Log Message:
-----------
[1.6] Added file missing from earlier commit
Tags:
----
mkdoc-1-6
Added Files:
-----------
mkd/flo/editor/Mixin:
file_size.pm
-------------- next part --------------
--- /dev/null
+++ flo/editor/Mixin/file_size.pm
@@ -0,0 +1,82 @@
+package flo::editor::Mixin::file_size;
+use strict;
+use warnings;
+
+=head1 NAME
+
+flo::editor::Mixin::file_size - mixin adding file size methods
+
+=head1 SYNOPSIS
+
+ # get size in bytes
+ $size = $image->file_size;
+
+ # print a human-readable size
+ print $image->formated_file_size();
+
+=head1 DESCRIPTION
+
+This mixin adds file-size methods to file, image and photo components.
+Two methods are available - file_size() which returns the size of the
+file in bytes and formated_file_size() which returns a human-readable
+size (ex: 12kb, 20mb).
+
+=head1 AUTHOR
+
+Sam Tregar <sam at tregar.com>
+
+=head1 COPYRIGHT
+
+Copyright MKDoc Holdings Ltd, 2005
+
+=head1 LICENSE
+
+MKDoc is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2 of the License, or (at your
+option) any later version.
+
+MKDoc is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with MKDoc; if not, write to the Free Software Foundation, Inc.,
+59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+=cut
+
+sub file_size {
+ my $self = shift;
+
+ # attachements and images store their files in different places
+ my $file;
+ if ($self->{file}) {
+ $file = MKDoc::Config->FILE_DISK_PATH . "/" . $self->{file};
+ } elsif ($self->{image}) {
+ $file = MKDoc::Config->IMAGE_DISK_PATH . "/" . $self->{image};
+ } else {
+ return 0;
+ }
+
+ return 0 unless -e $file;
+ return -s _;
+}
+
+sub formated_file_size {
+ my $self = shift;
+ my $size = $self->file_size;
+
+ if ($size > 0x100000) {
+ $size = int ($size / 0x100000) . "mb";
+ } elsif ($size > 0x400) {
+ $size = int ($size / 0x400) . "kb";
+ } else {
+ $size .= "b";
+ }
+
+ return $size;
+}
+
+1;
More information about the MKDoc-commit
mailing list