__init__.py 3.88 KB
Newer Older
Łukasz Nowak's avatar
Łukasz Nowak committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
##############################################################################
#
# Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################
import os
import sys
from flask import Flask, request, redirect
from optparse import OptionParser, Option
import logging
import logging.handlers


class Parser(OptionParser):
  """
  Parse all arguments.
  """
  def __init__(self, usage=None, version=None):
    """
    Initialize all options possibles.
    """
    OptionParser.__init__(self, usage=usage, version=version,
                          option_list=[
      Option("-l", "--log_file",
             help="The path of the log file",
             type=str,
             dest="log_file_path"),
    ])


  def check_args(self):
    """
    Check arguments
    """
    (options, args) = self.parse_args()
    if len(args) != 4:
      self.error("Incorrect number of arguments")
    host, port, upload_file, key = args

    upload_file = os.path.abspath(upload_file)

    return options, host, port, upload_file, key

class Config:
  def setConfig(self, option_dict, host, port, upload_file, key):
    """
    Set options given by parameters.
    """
    # Set options parameters
    for option, value in option_dict.__dict__.items():
      setattr(self, option, value)
    self.host = host
    self.port = int(port)
    self.upload_file = upload_file
    self.key = key

def run(config):
Cédric de Saint Martin's avatar
Cédric de Saint Martin committed
79 80 81 82
  """
  Will accept to upload a file only once. When a file has been uploaded,
  it will refuse any other upload.
  """
Łukasz Nowak's avatar
Łukasz Nowak committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

  app = Flask(__name__)

  if config.log_file_path is not None:
    file_handler = logging.handlers.RotatingFileHandler(
                config.log_file_path, maxBytes=500000, backupCount=5)

    file_handler.setLevel(logging.WARNING)

    app.logger.addHandler(file_handler)
    logging.getLogger('werkzeug').addHandler(file_handler)

  @app.route('/', methods=['GET', 'POST'])
  def upload_file():
    if os.path.exists(config.upload_file):
      template = app.open_resource('templates/done.html')
      return template.read()
    else:
      if request.method == 'POST':
        file = request.files['file']
        key = "%s" % request.form.get('key')
        if file and key:
          if key == config.key:
            file.save(config.upload_file)
            return redirect('/')
      template = app.open_resource('templates/index.html')
      return template.read()

111
  app.run(host=config.host, port=config.port)
Łukasz Nowak's avatar
Łukasz Nowak committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

def main():
  "Run default configuration."
  usage = "usage: onetimeupload [options] HOST PORT UPLOAD_FILE KEY"

  try:
    # Parse arguments
    config = Config()
    config.setConfig(*Parser(usage=usage).check_args())

    run(config)
    return_code = 0
  except SystemExit, err:
    # Catch exception raise by optparse
    return_code = err

  sys.exit(return_code)