Skip to content

Commit

Permalink
Item14062: only attach content when it changed
Browse files Browse the repository at this point in the history
Item11782: make sure content is properly encoded on a unicode Foswiki
  • Loading branch information
MichaelDaum committed May 3, 2016
1 parent 8224ea3 commit 2a8fddf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
14 changes: 7 additions & 7 deletions .gitignore
@@ -1,8 +1,8 @@
*.swp
AttachContentPlugin.md5
AttachContentPlugin.sha1
AttachContentPlugin.tgz
AttachContentPlugin.txt
AttachContentPlugin.zip
AttachContentPlugin_installer
AttachContentPlugin_installer.pl
/AttachContentPlugin.md5
/AttachContentPlugin.sha1
/AttachContentPlugin.tgz
/AttachContentPlugin.txt
/AttachContentPlugin.zip
/AttachContentPlugin_installer
/AttachContentPlugin_installer.pl
12 changes: 7 additions & 5 deletions data/System/AttachContentPlugin.txt
@@ -1,7 +1,6 @@
%META:TOPICINFO{author="ProjectContributor" date="1127466547" format="1.1" reprev="1.1" version="1.1"}%
%META:TOPICINFO{author="ProjectContributor" date="1462265132" format="1.1" version="1"}%
---+!! Attach Content Plugin

%$SHORTDESCRIPTION%
%FORMFIELD{"Description"}%

%TOC%

Expand Down Expand Up @@ -99,6 +98,8 @@ After installation, configure this plugin by changing settings in [[%SCRIPTURL{c
---++ Change History

%TABLE{columnwidths="7em" tablewidth="100%"}%
| 03 May 2016 (2.40) | Foswikitask:Item14062 - only attach content when it actually changed;\
Foswikitask:Item11782 - make sure the file is proper utf8 on unicode Foswiki |
| 25 Sep 2015 (2.34) | Foswikitask:Item13747 - fixed unescaped left braces; \
use CPAN:File::Temp for temporary files; check access rights before saving auto-created attachment to prevent an unnecessary error condition; \
check access rights before saving auto-created attachment to prevent an unnecessary error condition; \
Expand All @@ -118,9 +119,10 @@ After installation, configure this plugin by changing settings in [[%SCRIPTURL{c
%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="ProjectContributor"}%
%META:FIELD{name="Copyright" title="Copyright" value="© TWiki:Main.MeredithLesly, Foswiki:Main.KennethLavrsen, Foswiki:Main.ArthurClemens"}%
%META:FIELD{name="Home" title="Home" value="http://foswiki.org/Extensions/%TOPIC%"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Home" title="Home" value="https://foswiki.org/Extensions/%TOPIC%"}%
%META:FIELD{name="License" title="License" value="GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]])"}%
%META:FIELD{name="Release" title="Release" value="%$RELEASE%"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/AttachContentPlugin"}%
%META:FIELD{name="Support" title="Support" value="http://foswiki.org/Support/%TOPIC%"}%
%META:FIELD{name="Support" title="Support" value="https://foswiki.org/Support/%TOPIC%"}%
%META:FIELD{name="Version" title="Version" value="%$VERSION%"}%
60 changes: 34 additions & 26 deletions lib/Foswiki/Plugins/AttachContentPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (c) 2015 Foswiki Contributors
# Copyright (c) 2015,2016 Foswiki Contributors
# Copyright (c) 2007,2009 Arthur Clemens
# Copyright (c) 2006 Meredith Lesly, Kenneth Lavrsen
# and TWiki Contributors. All Rights Reserved.
Expand All @@ -26,9 +26,10 @@ use strict;
use warnings;
use Foswiki::Func ();
use File::Temp();
use Digest::MD5 qw(md5_hex);

our $VERSION = '2.34';
our $RELEASE = '24 Sep 2015';
our $VERSION = '2.40';
our $RELEASE = '03 May 2016';
our $SHORTDESCRIPTION = 'Saves dynamic topic text to an attachment';
our $NO_PREFS_IN_TOPIC = 1;

Expand Down Expand Up @@ -175,10 +176,6 @@ sub _handleAttach {
Foswiki::Func::sanitizeAttachmentName($attrFileName);
_debug("\t fileName=$fileName");

my $fh = File::Temp->new();
my $tempName = $fh->filename;
_debug("\t tempName: $tempName");

# Turn most TML to text
my $content =
Foswiki::Func::expandCommonVariables( $inContent, $topic, $web );
Expand All @@ -196,25 +193,36 @@ sub _handleAttach {
$content =~ s/[[:space:]]+$//s; # trim at end
($content) ? _debug("\t content: $content") : _debug("\t no content");

# Saving temporary file
Foswiki::Func::saveFile( $tempName, $content );

my @stats = stat $tempName;
my $fileSize = $stats[7];
my $fileDate = $stats[9];

Foswiki::Func::saveAttachment(
$web, $topic,
$fileName,
{
file => $tempName,
filedate => $fileDate,
filesize => $fileSize,
filepath => $fileName,
comment => $comment,
hide => $hide
}
);
my $newMD5 = md5_hex($content);
my $oldContent = Foswiki::Func::readAttachment($web, $topic, $fileName);
my $oldMD5 = md5_hex($oldContent);

if ($newMD5 ne $oldMD5) {

my $fh = File::Temp->new();
my $tempName = $fh->filename;
_debug("\t tempName: $tempName");

# Saving temporary file
Foswiki::Func::saveFile( $tempName, $content, 1 );

my @stats = stat $tempName;
my $fileSize = $stats[7];
my $fileDate = $stats[9];

Foswiki::Func::saveAttachment(
$web, $topic,
$fileName,
{
file => $tempName,
filedate => $fileDate,
filesize => $fileSize,
filepath => $fileName,
comment => $comment,
hide => $hide
}
);
}

return '';
}
Expand Down

0 comments on commit 2a8fddf

Please sign in to comment.