Skip to content

Commit

Permalink
Item14470: add REST service to get icons
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDaum committed Aug 30, 2017
1 parent 6885dc7 commit a76eae1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
igp_*
*.swp
*.gz
/MimeIconPlugin.md5
Expand Down
25 changes: 24 additions & 1 deletion data/System/MimeIconPlugin.txt
Expand Up @@ -40,6 +40,28 @@ The idea is that you can pretty much throw any file at it and =%MIMEICON= will r
reflecting the mimetype of this file. Well, obviously there is a fallback to a neutral icon in case some
file extension is totally unknown.

---++ REST service

The =get= endpoint allows to retrieve a proper icon for a given mime type or file.

| *Parameter* | *Description* | *Default* |
| =file= | file or file extension for which to retrieve a matching icon | |
| =size= | size of icon | 48 |
| =theme= | icon set | =$Foswiki::cfg{Plugins}{MimeIconPlugin}{Theme}= |

Calling the =get= service will create a redirect to the proper icon url in a way that it can be used in an IMG html tag

<verbatim class="tml">
<img src="%SCRIPTURLPATH{"rest"}%/MimeIconPlugin/get?file=foobar.docx;size=48" />
</verbatim>

redirects to

<verbatim>
.../pub/System/MimeIconPlugin/oxygen/48x48/application-vnd.oasis.opendocument.text.png
</verbatim>


---++ Examples

---+++ Oxygen
Expand All @@ -55,6 +77,7 @@ file extension is totally unknown.
%$DEPENDENCIES%

---++ Change History
| 30 Aug 2017: | (2.00) added =get= REST handler |
| 09 Sep 2016: | (1.31) added vsd to image mapping |
| 17 Jul 2015: | (1.30) added support for google file formats webp, weba, webm |
| 22 May 2013: | (1.20) only ship icons covered by a mapping |
Expand All @@ -64,7 +87,7 @@ file extension is totally unknown.
%META:FILEATTACHMENT{name="MimeIconPluginSnap1.png" attachment="MimeIconPluginSnap3.png" attr="" comment="" date="1437155075" moveby="micha" movedto="System.MimeIconPlugin.MimeIconPluginSnap1.png" movedwhen="1369210422" movefrom="System.MimeIconPlugin.MimeIconPluginSnap3.png" size="18836" user="ProjectContributor" version="1"}%
%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="Michael Daum"}%
%META:FIELD{name="Copyright" title="Copyright" value="&copy; 2010-2016 Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="Copyright" title="Copyright" value="&copy; 2010-2017 Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Home" title="Home" value="Foswiki:Extensions/%TOPIC%"}%
%META:FIELD{name="License" title="License" value="[[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]]"}%
Expand Down
50 changes: 45 additions & 5 deletions lib/Foswiki/Plugins/MimeIconPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# MimeIconPlugin is Copyright (C) 2010-2016 Michael Daum http://michaeldaumconsulting.com
# MimeIconPlugin is Copyright (C) 2010-2017 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
Expand All @@ -26,8 +26,8 @@ use warnings;

use Foswiki::Func ();

our $VERSION = '1.31';
our $RELEASE = '09 Sep 2016';
our $VERSION = '2.00';
our $RELEASE = '30 Aug 2017';
our $SHORTDESCRIPTION = 'Icon sets for mimetypes';
our $NO_PREFS_IN_TOPIC = 1;
our $baseWeb;
Expand All @@ -45,11 +45,52 @@ sub initPlugin {

Foswiki::Func::registerTagHandler('MIMEICON', \&MIMEICON);

Foswiki::Func::registerRESTHandler(
'get',
\&handleREST,
authenticate => 0,
validate => 0,
http_allow => 'GET,POST',
);


return 1;
}

=begin TML
--++ handleREST
returns the url to a mime icon
=cut

sub handleREST {
my ($subject, $verb, $response) = @_;

my $query = Foswiki::Func::getCgiQuery();
my $file = $query->param("file") || '';

$file =~ s/^.*\.//;
$file =~ s/^\s+|\s+$//g;

my $size = $query->param("size") || '48';
my $theme = $query->param("theme");

$theme = $Foswiki::cfg{Plugins}{MimeIconPlugin}{Theme} || 'oxygen'
unless defined $theme;

$size = getBestSize($theme, $size);

my (undef, $iconPath) = getIcon($file, $theme, $size);

Foswiki::Func::redirectCgiQuery($query, $iconPath);

return "";
}

=begin TML
---++ MIMEICON
handler for the MIMEICON macro
Expand All @@ -71,8 +112,7 @@ sub MIMEICON {
unless defined $theme;

$extension =~ s/^.*\.//;
$extension =~ s/^\s+//;
$extension =~ s/\s+$//;
$extension =~ s/^\s+|\s+$//g;

$size = getBestSize($theme, $size);
my ($iconName, $iconPath) = getIcon($extension, $theme, $size);
Expand Down
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/MimeIconPlugin/MANIFEST
@@ -1,3 +1,4 @@
!noci
data/System/MimeIconPlugin.txt 0644
data/System/VarMIMEICON.txt 0644
lib/Foswiki/Plugins/MimeIconPlugin/Config.spec 0644
Expand Down
2 changes: 2 additions & 0 deletions pub/System/MimeIconPlugin/oxygen/mapping.txt
Expand Up @@ -2,6 +2,7 @@ sizes=16, 22, 32, 48, 64, 128, 256

ill=application-illustrator.png
js=application-javascript.png
json=application-javascript.png
doc=application-msword.png
dot=application-msword.png
bin=application-octet-stream.png
Expand Down Expand Up @@ -264,6 +265,7 @@ txt=text-plain.png
text=text-plain.png
rdf=text-rdf.png
rtf=text-rtf.png
sass=text-css.png
sglm=text-sgml.png
troff=text-troff.png
vcs=text-vcalendar.png
Expand Down

0 comments on commit a76eae1

Please sign in to comment.