There is no way to read/write attachments via the plugins API. This is urgently required, otherwise plugins authors will be forced to continue to violate the Store encapsulation and read and write files directly.
A list of attachments on a topic is available from the
$meta read when
readTopic is used. So we need:
readAttachment( $meta, $name ) -> $data
-
$meta - meta-data for the topic
-
$name - name of the attachment within the topic
Read an attachment from the store for a topic, and return it as a string. The names of attachments on a topic can be recovered from the meta-data returned by
readTopic. If the attachment does not exist, or cannot be read, undef will be returned.
my( $meta, $text ) = TWiki::Func::readTopic( $web, $topic );
my @attachments = $meta->find( 'FILEATTACHMENT' );
foreach my $a ( @attachments ) {
my $data = TWiki::Func::readAttachment( $meta, $a->{name} );
...
}
writeAttachment( $meta, $name, $data ) -> $error
-
$meta - meta-data for the topic where the attachment is to be saved. This meta will be modified to reflect the changed attachment.
-
$name - name of the attachment within the topic
-
$data - data to be stored in the attachment
Write an attachment to store for the given web.topic. $meta will be updated if necessary, but will
not be written back until you call
writeTopic. On success, returns undef. If there is an error, a report string will be returned.
my( $meta, $text ) = TWiki::Func::readTopic( $web, $topic );
my @attachments = $meta->find( 'FILEATTACHMENT' );
foreach my $a ( @attachments ) {
my $data = TWiki::Func::readAttachment( $meta, $a->{name} );
...munge the data...
my $e = TWiki::Func::writeAttachment( $meta, $a->{name}, $data );
if( $e ) {
die "Error while writing attachment '.$a->{name}.' to '.$meta->web().'.'.$meta->topic().': '.$e;
}
}
TWiki::Func::writeTopic( $web, $topic, $meta, $text ); # save any changes to the meta-data
--
CrawfordCurrie - 15 Aug 2005