r3 - 09 Mar 2006 - 17:37:06 - CrawfordCurrieYou are here: TWiki >  Codev Web > ReadWriteAttachmentsFromFuncAPI
Tags:
, create new tag
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

 
Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r3 < r2 < r1 | More topic actions
 
Powered by TWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback SourceForge.net Logo