#!/usr/bin/w3tclsh
#
# This script demonstrates a W3 application that can accept a file
# upload using <input type="file">
#
package require w3
proc common-header {} {
  w3-trim {
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link href="%url([w3-param SCRIPT_NAME]/style.css)" rel="stylesheet">
    <title>W3 File-Upload Demo</title>
    </head>
    <body>
  }
}
proc common-footer {} {
  w3-trim {
    </body>
    </html>
  }
}
proc w3-default {} {
  w3-content-security-policy {default-src 'self'; img-src 'self' data:}
  common-header
  w3-trim {
    <h1>W3 File-Upload Demo</h1>
  }
  # NB:  You must set enctype="multipart/form-data" on your <form> in order
  # for file upload to work.
  w3-trim {
    <p><form method="POST" enctype="multipart/form-data">
    File To Upload: <input type="file" name="file"><br>
    <input type="checkbox" name="showenv" value="1">Show CGI Environment<br>
    <input type="hidden" name="PARAM1"
     value="Post parameter with non-lowercase names are suppressed">
    <input type="hidden" name="param2.value"
     value="Post parameters with non-lowercase names are suppressed">
    <input type="submit" value="Submit">
    </form></p>
    <p><a href='%html([w3-param SCRIPT_NAME])/self'>Show the script
    that generates this page</a></p>
  }
  # Ordinary query parameters come through just like normal
  if {[w3-param showenv 0]} {
    w3-trim {
      <h1>W3 Environment</h1>
      <pre>%html([w3-debug-env])</pre>
    }
  }
  # File upload query parameters come in three parts:  The *.mimetype,
  # the *.filename, and the *.content.
  set mimetype [w3-param file.mimetype {}]
  set filename [w3-param file.filename {}]
  set content [w3-param file.content {}]
  if {$filename!=""} {
    w3-trim {
      <h1>Uploaded File Content</h1>
      <p>Filename: %html($filename)<br>
      MIME-Type: %html($mimetype)<br>
    }
    if {[string match image/* $mimetype]} {
      # If the mimetype is an image, display the image using an
      # in-line <img> mark.  Note that the content-security-policy
      # must be changed to allow "data:" for type img-src in order
      # for this to work.
      set b64 [binary encode base64 $content]
      w3-trim {
        Content:</p>
        <blockquote>
        <img src='data:%html($mimetype);base64,%html($b64)'>
        </blockquote>
      }
    } else {
      # Anything other than image, just show it as text.
      w3-trim {
        Content:</p>
        <blockquote><pre>
        %html($content)
        </pre></blockquote>
      }
    }
  }
  common-footer
}
proc w3-page-self {} {
  w3-cache-control max-age=3600
  common-header
  set fd [open [w3-param SCRIPT_FILENAME] rb]
  set script [read $fd]
  close $fd
  w3-trim {
    <h1>W3 Script That Shows A Copy Of Itself</h1>
    <pre>%html($script)</pre>
  }
  common-footer
}
proc w3-page-style.css {} {
  w3-mimetype text/css
  w3-cache-control max-age=3600
  w3-trim {
    pre {
       border: 1px solid black;
       padding: 1ex;
    }
  }
}
w3-start $argv
