| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Kwiki::UserPhoto; |
|
2
|
1
|
|
|
1
|
|
1547
|
use Kwiki::Plugin qw(-Base -XXX); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use mixin 'Kwiki::Installer'; |
|
4
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
const class_id => 'user_photo'; |
|
7
|
|
|
|
|
|
|
const class_title => 'User Photo'; |
|
8
|
|
|
|
|
|
|
const css_file => 'user_photo.css'; |
|
9
|
|
|
|
|
|
|
const cgi_class => 'Kwiki::UserPhoto::CGI'; |
|
10
|
|
|
|
|
|
|
const config_file => 'user_photo.yaml'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
field 'display_msg'; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub register { |
|
15
|
|
|
|
|
|
|
my $registry = shift; |
|
16
|
|
|
|
|
|
|
$registry->add(widget => 'user_photo', |
|
17
|
|
|
|
|
|
|
template => 'user_photo_widget.html'); |
|
18
|
|
|
|
|
|
|
$registry->add(action => 'user_photo'); |
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Return the url of current user. |
|
22
|
|
|
|
|
|
|
sub url { |
|
23
|
|
|
|
|
|
|
my $user = $self->hub->users->current->name; |
|
24
|
|
|
|
|
|
|
my $dir = io->catdir($self->plugin_directory,$user); |
|
25
|
|
|
|
|
|
|
if($dir->exists) { |
|
26
|
|
|
|
|
|
|
for($dir->all) { |
|
27
|
|
|
|
|
|
|
return $_->name if($_->name =~ /thumb./); |
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
} |
|
30
|
|
|
|
|
|
|
return $self->config->user_photo_default; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub user_photo { |
|
34
|
|
|
|
|
|
|
my $mode = $self->cgi->run_mode; |
|
35
|
|
|
|
|
|
|
# Valid modes: upload |
|
36
|
|
|
|
|
|
|
$self->$mode if($mode && $self->can($mode)); |
|
37
|
|
|
|
|
|
|
$self->render_screen; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub upload { |
|
41
|
|
|
|
|
|
|
require YAML; |
|
42
|
|
|
|
|
|
|
my $username = $self->users->current->name; |
|
43
|
|
|
|
|
|
|
my $file = $self->cgi->uploaded_file; |
|
44
|
|
|
|
|
|
|
return $self->display_msg("You must setup a proper user name to upload your photo") unless($username); |
|
45
|
|
|
|
|
|
|
if($file){ |
|
46
|
|
|
|
|
|
|
my $fh = $file->{handle}; |
|
47
|
|
|
|
|
|
|
my $newfile = io->catfile($self->plugin_directory, $username, $file->{filename}); |
|
48
|
|
|
|
|
|
|
$_->unlink for io->catdir($self->plugin_directory,$username)->assert->all; |
|
49
|
|
|
|
|
|
|
binmode($fh); |
|
50
|
|
|
|
|
|
|
$newfile->assert->print(<$fh>); |
|
51
|
|
|
|
|
|
|
$newfile->close(); |
|
52
|
|
|
|
|
|
|
$self->make_thumbnail($newfile, |
|
53
|
|
|
|
|
|
|
$self->config->user_photo_width, |
|
54
|
|
|
|
|
|
|
$self->config->user_photo_height, |
|
55
|
|
|
|
|
|
|
); |
|
56
|
|
|
|
|
|
|
} else { |
|
57
|
|
|
|
|
|
|
$self->display_msg("Please specify a file to upload."); |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# From Kwiki::Attachments |
|
62
|
|
|
|
|
|
|
sub make_thumbnail { |
|
63
|
|
|
|
|
|
|
use File::Basename; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my ($file,$width,$height) = @_; |
|
66
|
|
|
|
|
|
|
my ($fname, $fpath, $ftype) = fileparse($file, qr(\..*$)); |
|
67
|
|
|
|
|
|
|
my $thumb = io->catfile($fpath, "thumb$ftype"); |
|
68
|
|
|
|
|
|
|
if (eval { require Imager }) { |
|
69
|
|
|
|
|
|
|
my $found = 0; |
|
70
|
|
|
|
|
|
|
if ($ftype =~ /jpg/i) { |
|
71
|
|
|
|
|
|
|
$found = 1; |
|
72
|
|
|
|
|
|
|
} else { |
|
73
|
|
|
|
|
|
|
for (keys %Imager::format) { |
|
74
|
|
|
|
|
|
|
if ($ftype =~ /$_/oi) { |
|
75
|
|
|
|
|
|
|
$found = 1; |
|
76
|
|
|
|
|
|
|
last; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
if ($found) { |
|
81
|
|
|
|
|
|
|
my $image = Imager->new; |
|
82
|
|
|
|
|
|
|
return unless ref($image); |
|
83
|
|
|
|
|
|
|
$image->read(file=>$file); |
|
84
|
|
|
|
|
|
|
my $thumb_img = $image->scale(xpixels=>$width,ypixels=>$height); |
|
85
|
|
|
|
|
|
|
$thumb_img->write(file=>$thumb); |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
} elsif (eval { require Image::Magick }) { |
|
88
|
|
|
|
|
|
|
my $image = Image::Magick->new; |
|
89
|
|
|
|
|
|
|
return unless ref($image); |
|
90
|
|
|
|
|
|
|
if (!$image->Read($file)) { |
|
91
|
|
|
|
|
|
|
if (!$image->Scale(geometry=>"${width}x${height}")) { |
|
92
|
|
|
|
|
|
|
if (!$image->Contrast(sharpen=>"true")) { |
|
93
|
|
|
|
|
|
|
$image->Write($thumb); |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
package Kwiki::UserPhoto::CGI; |
|
101
|
|
|
|
|
|
|
use base 'Kwiki::CGI'; |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
cgi 'run_mode'; |
|
104
|
|
|
|
|
|
|
cgi 'uploaded_file' => qw(-upload); |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
package Kwiki::UserPhoto; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__DATA__ |