-
Notifications
You must be signed in to change notification settings - Fork 3
/
CreateLinks.pm
230 lines (176 loc) · 6.07 KB
/
CreateLinks.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2010-2018 Michael Daum, http://michaeldaumconsulting.com
#
# This program 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.
#
# This program 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, published at
# http://www.gnu.org/copyleft/gpl.html
package Foswiki::Plugins::TopicInteractionPlugin::Action::CreateLinks;
use strict;
use warnings;
use Error qw( :try );
use Foswiki::Func ();
use Foswiki::Plugins ();
use Foswiki::Plugins::TopicInteractionPlugin::Action ();
our @ISA = ('Foswiki::Plugins::TopicInteractionPlugin::Action');
use constant DRY => 0; # toggle me
sub handle {
my ($this, $response) = @_;
my $params = $this->prepareAction($response);
return unless $params;
my $web = $params->{web};
my $topic = $params->{topic};
my $id = $params->{id};
my $doHideFile = Foswiki::Func::isTrue($params->{hidefile}, 0);
# check permissions
my $wikiName = Foswiki::Func::getWikiName();
unless (Foswiki::Func::checkAccessPermission(
'CHANGE', $wikiName, undef, $topic, $web)) {
$this->printJSONRPC($response, 102, "Access denied", $id);
return;
}
my ($meta, $text) = Foswiki::Func::readTopic($web, $topic);
$text = '' unless defined $text;
my $error;
foreach my $fileName (@{$params->{filenames}}) {
next unless $fileName;
$fileName = $this->sanitizeAttachmentName($fileName);
my $attachment = $meta->get("FILEATTACHMENT", $fileName);
unless ($attachment) {
$this->printJSONRPC($response, 104, "Attachment $fileName does not exist", $id);
return;
}
if ($doHideFile) {
my %attrs = map {$_ => 1} split(//, ($attachment->{attr} || ''));
$attrs{h} = 1;
$attachment->{attr} = join("", sort keys %attrs);
}
$this->writeDebug("createlink fileName=$fileName, web=$web, topic=$topic");
$text .= $this->getAttachmentLink($meta, $fileName);
}
try {
unless (DRY) {
$meta->text($text);
$meta->save();
}
} catch Error::Simple with {
$error = shift->{-text};
$this->writeDebug("ERROR: $error");
};
if ($error) {
$this->printJSONRPC($response, 1, $error, $id);
} else {
$this->printJSONRPC($response, 0, undef, $id)
}
}
sub getAttachmentLink {
my ($this, $meta, $fileName) = @_;
my $attachment = $meta->get('FILEATTACHMENT', $fileName);
my $fileComment = $attachment->{comment} // '';
my $fileTime = Foswiki::Func::formatTime($attachment->{date} || 0);
my $filePath = $Foswiki::cfg{PubDir} . '/' . $meta->web . '/' . $meta->topic . '/' . $fileName;
my ($fileExt) = $fileName =~ m/(?:.*\.)*([^.]*)/;
$fileExt //= '';
my $width = "";
my $height = "";
my $geom = "";
my $format = $this->getAttachmentFormat($fileName);
# only support values if ImagePlugin is installed
if ($format =~ /\$width|\$height|\$size/) {
($width, $height) = $this->ping($filePath);
$geom = "width='$width' height='$height'";
}
$format =~ s/\$name/$fileName/; # deprecated
$format =~ s/\$filename/$fileName/g;
$format =~ s/\$fileurl/$fileName/g;
$format =~ s/\$fileext/$fileExt/;
# SMELL: backwards compatibility ...
$format =~ s/\\t/\t/g;
$format =~ s/\\n/\n/g;
$format =~ s/\$comment/$fileComment/g;
$format =~ s/\$size/$geom/g;
# new
$format =~ s/\$width/$width/g;
$format =~ s/\$height/$height/g;
$format =~ s/\$date/$fileTime/g;
# this is deliberatley orderd that way to prevent some makros from being executed
$format = Foswiki::Func::expandCommonVariables($format);
$format = Foswiki::Func::decodeFormatTokens($format);
return $format;
}
sub ping {
my ($this, $filePath) = @_;
eval "require Foswiki::Plugins::ImagePlugin";
return if $@;
return Foswiki::Plugins::ImagePlugin::getCore()->mage->Ping($filePath);
}
sub getAttachmentFormat {
my ($this, $fileName) = @_;
my $format;
my $prefName;
if ($fileName =~ /(?:.*\.)*([^.]*)/) {
$prefName = 'ATTACHED_'.uc($1).'_FORMAT';
$format = Foswiki::Func::getPreferencesValue($prefName);
}
unless ($format) {
my ($type) = $this->getMappedMimeType($fileName);
if ($type) {
$prefName = 'ATTACHED_'.uc($type).'_FORMAT';
$format = Foswiki::Func::getPreferencesValue($prefName);
# backwards compatibility
if ($type eq 'image' && !$format) {
$prefName = 'ATTACHEDIMAGEFORMAT';
$format = Foswiki::Func::getPreferencesValue($prefName);
}
}
}
unless ($format) {
$format = Foswiki::Func::getPreferencesValue('ATTACHED_FILE_FORMAT');
$format = Foswiki::Func::getPreferencesValue('ATTACHEDFILELINKFORMAT') unless $format; # backwards compatibility
}
$format = ' * [[$percntATTACHURLPATH$percnt/$filename][$filename]]: $comment' unless $format;
return $format;
}
sub types {
my $this = shift;
$this->{_types} = Foswiki::Func::readFile($Foswiki::cfg{MimeTypesFileName}) unless defined $this->{_types};
$this->{_types} //= "";
return $this->{_types};
}
sub getMimeType {
my ($this, $fileName) = @_;
my $mimeType;
my $suffix = $fileName;
if ($fileName =~ /\.([^.]+)$/) {
$suffix = $1;
}
if ($this->types =~ /^([^#]\S*).*?\s$suffix(?:\s|$)/im) {
$mimeType = $1;
}
return unless defined $mimeType;
my ($type, $subType) = $mimeType =~ /^(.*)\/(.*)$/;
return wantarray ? ($type, $subType) : $mimeType;
}
sub getMappedMimeType {
my ($this, $fileName) = @_;
my ($type, $subType) = $this->getMimeType($fileName);
return unless defined $type;
if ($type eq 'application') {
if ($subType =~ /document|msword|msexcel|rtf/) {
$type = 'document';
} elsif ($subType =~ /pdf|postscript/) {
$type = 'pdf';
} elsif ($subType =~ /xcf/) {
$type = 'image';
}
}
return wantarray ? ($type, $subType) : "$type/$subType";
}
1;