create new tag
, view all tags

Implemented: Include attachments directly, without an http request

Including an attached file using %INCLUDE{"%ATTACHURL%/file.htm"}% has two problems:

  1. It doesn't work if the proxy is set up wrong - for example, it doesn't work on sourceforge.
  2. It is inefficient; it has to make an http: request and download for a simple file access.

This kind of include is very common in a corporate environment where external applications (often cron jobs) are used to generate HTML reports that need to be presented in TWiki pages, so this needs to be efficient.

This feature enhancement reads attached files directly if the URL matches the URL of an attached file. This is very quick since it does not require an http request.

An error message is returned if the user has no permission to view a topic where the attached file resides. %ATTACHURL%/file.htm


  • You cannot include an attachment from another topic with view restriction in case the user is not authenticated.
  • The criteria for including a file is by file suffix .txt, .htm and .html. This should rather be done with MIME types

-- CrawfordCurrie - 22 Mar 2004 -- PeterThoeny - 18 Apr 2004

The following patch extends %INCLUDE to support the server-side inclusion of text and html files attached to the current topic i.e. in the pub directory for this topic. It is quite trivial, so I present it here in-line. Base version TWiki.pm version 1.307, tested against code in CVS 22/3/04.

cvs diff -u TWiki.pm
cvs diff: warning: failed to open /root/.cvspass for reading: No such file or directory
Index: TWiki.pm
RCS file: /cvsroot/twiki/twiki/lib/TWiki.pm,v
retrieving revision 1.307
diff -u -r1.307 TWiki.pm
--- TWiki.pm    20 Mar 2004 10:51:33 -0000      1.307
+++ TWiki.pm    22 Mar 2004 11:59:23 -0000
@@ -1908,6 +1908,15 @@
         $incfile =~ s/passwd//gi;    # filter out passwd filename

+    if( $incfile =~ m/\.(txt|html?)$/o ) {
+      # file from pub directory
+      my $fileName = "$pubDir/$theWeb/$theTopic/$incfile";
+      if( -e $fileName ) {
+       # one of our attachments; read and return
+       return TWiki::Store::readFile( $fileName );
+      }
+    }
     # test for different usage
     my $fileName = "$dataDir/$theWeb/$incfile";       # TopicName.txt
     if( ! -e $fileName ) {

Note that discussion on TWikiIRC suggests that supporting an attachment parameter to INCLUDE would actually be the best long-term solution, as it would support including attachments from other topics. However this is not a trivial change, as it has all the associated problems of access controls, and if it were done I would contend that the whole of handleIncludeFile should be re-written. It has the following problems:

  1. pattern does not work on URL-includes
  2. support for .. has been hacked out without removing all the support in the function
While I could have re-written handleIncludeFile to fix this, I felt that the patch presented above is an incredibly useful and low-risk measure.

-- CrawfordCurrie - 22 Mar 2004

That is a useful feature. However, I find the syntax confusing because we mix topic names with attachment names.

A better approach is to require the usual URL pointing to an attached file, but to read the attachment directly instead of issuing an http request.

I created a patch accordingly; it is attached to this topic, is in TWikiAlphaRelease and on TWiki.org.

Test: %INCLUDE{%ATTACHURL%/test.txt}% in pre tags shows this:

This is line one
and line 2

Could someone help out with the docs?

-- PeterThoeny - 18 Apr 2004

User docs done (see TWikiVariables). Code docs I would do except I can't check in to core, and Sven already has enough to do as the only active core team member at the moment. Marking docs 99% done

-- CrawfordCurrie - 30 Jun 2004

This didn't work for me with TWikiRelease01Sep2004. My server uses ssl, so all urls start https://


    if( $incfile =~ /^http\:/ ) {
        # include web page
        return handleIncludeUrl( $incfile, $pattern, $theWeb, $theTopic );

Means that my urls are never matched. Changing this to:

    if( $incfile =~ /^(http|https)\:/ ) {
        # include web page
        return handleIncludeUrl( $incfile, $pattern, $theWeb, $theTopic );

fixes this problem.

-- DaveKnight - 19 Nov 2004

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatpatch incattach.patch r1 manage 10.5 K 2004-04-18 - 06:45 PeterThoeny Include attachment patch
Unknown file formatdiff patch.diff r1 manage 0.8 K 2004-03-22 - 13:41 CrawfordCurrie  
Texttxt test.txt r1 manage 0.1 K 2004-04-18 - 07:12 PeterThoeny Testing include of attached file
Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2004-11-19 - DaveKnight
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2018 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.