Example #1
0
def get_file(filename):
  print_log('Web', 'Hit "' + filename + '" - ' + time_to_string(time.time()))
  try:
    db.update_file(filename)
  except Exception:
    print_log('Warning', 'Unable to update access time. Is the file in the database?')
  return send_from_directory(config['UPLOAD_FOLDER'], filename)
Example #2
0
def get_file(filename):
  print_log('Web', 'Hit "' + filename + '"')
  try:
    db.update_file(filename)
  except Exception:
    print_log('Warning', 'Unable to update access time. Is the file in the database?')
  return send_from_directory(config['UPLOAD_FOLDER'], filename)
Example #3
0
def cleaner_thread():
  # Call itself again after the interval
  cleaner = Timer(config["CLEAN_INTERVAL"], cleaner_thread)
  cleaner.daemon = True # Daemons will attempt to exit cleanly along with the main process, which we want
  cleaner.start()
  print_log('Thread', 'Cleaner prepared', print_debug)
  # Actual function
  delete_old()
Example #4
0
def allowed_file(file):
  if config["ALLOW_ALL_FILES"]:
    print_log('Main', 'All files permitted, no check done', print_debug)
    return True
  else:
    if config["BLACKLIST"]:
      if magic.from_buffer(file.read(1024), mime=True) not in config["BANNED_MIMETYPES"]:
        file.seek(0) # seek back to start so a valid file could be read
        return True
      else:
        file.seek(0)
        return False
    else:
      if magic.from_buffer(file.read(1024), mime=True) in config["ALLOWED_MIMETYPES"]:
        file.seek(0)
        return True
      else:
        file.seek(0)
        return False
Example #5
0
def delete_old():
  print_log('Notice', 'Cleaner running')
  targetTime = time.time() - config["TIME"]
  old = db.get_old_files(targetTime)
  for file in old:
    print_log('Notice', 'Removing old file "' + file["file"] + '"')
    try:
      os.remove(os.path.join(config["UPLOAD_FOLDER"], file["file"]))
    except Exception:
      print_log('Warning', 'Failed to delete old file "' + file["file"] + '"')
    db.delete_entry(file["file"])
Example #6
0
def delete_old():
  print_log('Thread', 'Cleaner running', print_debug)
  targetTime = time.time() - config["TIME"]
  old = db.get_old_files(targetTime)
  for file in old:
    print_log('Thread', 'Removing old file "' + file["file"] + '"')
    try:
      os.remove(os.path.join(config["UPLOAD_FOLDER"], file["file"]))
    except Exception:
      print_log('Warning', 'Failed to delete old file "' + file["file"] + '"')
    db.delete_entry(file["file"])
Example #7
0
def upload_file():
    if request.method == 'POST':
        print_log('Web', 'New file received')
        if not application.basicauth(request.headers.get('X-Hyozan-Auth'),
                                     config["KEY"]):
            abort(403)
        data = dict()
        file = request.files['file']

        # Only continue if a file that's allowed gets submitted.
        if file and allowed_file(file.filename):
            filename = secure_filename(
                short_url.encode_url(int(time.time()), 5) + '.' +
                file.filename.rsplit('.', 1)[1])
            while os.path.exists(
                    os.path.join(config["UPLOAD_FOLDER"], filename)):
                filename = str(randint(1000,
                                       8999)) + '-' + secure_filename(filename)

            thread1 = Thread(target=db.add_file, args=(filename, ))
            thread1.start()
            print_log('Thread', 'Adding to DB')
            file.save(os.path.join(config['UPLOAD_FOLDER'], filename))
            thread1.join()

            data["file"] = filename
            data["url"] = config["DOMAIN"] + "/" + filename
            print_log('Main', 'New file processed "' + filename + '"')

            try:
                if request.form["source"] == "web":
                    return render_template('link.html',
                                           data=data,
                                           page=config["SITE_DATA"])
            except Exception:
                return json.dumps(data)
        else:
            print_log('Notice', 'Forbidden file received')
            return error_page(error="This file isn't allowed, sorry!",
                              code=403)

    # Return Web UI if we have a GET request
    elif request.method == 'GET':
        return render_template('upload.html', page=config["SITE_DATA"])
Example #8
0
def upload_file():
  if request.method == 'POST':
    print_log('Web', 'New file received')
    if not auth(request.headers.get('X-Hyozan-Auth')):
      abort(403)
    data = dict()
    file = request.files['file']

    # Only continue if a file that's allowed gets submitted.
    if file and allowed_file(file.filename):
      filename = secure_filename(file.filename)
      while os.path.exists(os.path.join(config["UPLOAD_FOLDER"], filename)):
        filename = str(randint(1000,8999)) + '-' + secure_filename(filename)

      thread1 = Thread(target = db.add_file, args = (filename,))
      thread1.start()
      print_log('Thread', 'Adding to DB')
      file.save(os.path.join(config['UPLOAD_FOLDER'], filename))
      thread1.join()

      data["file"] = filename
      data["url"] = config["DOMAIN"] + "/" + filename
      print_log('Main', 'New file processed "' + filename + '"')

      try:
        if request.form["source"] == "web":
          return render_template('link.html', data=data, page=config["SITE_DATA"])
      except Exception:
        return json.dumps(data)
    else:
      print_log('Notice', 'Forbidden file received')
      return error_page(error="This file isn't allowed, sorry!", code=403)

  # Return Web UI if we have a GET request
  elif request.method == 'GET':
    return render_template('upload.html', page=config["SITE_DATA"])
Example #9
0
import json
import time
from random import randint

# Import our configuration
from conf import config

# Import QuadFile stuff
from QuadFile import db
from QuadFile.output import print_log, time_to_string

app = Flask(__name__)


# TODO: Try to turn these into functions or something I dunno
print_log('Main', 'Running in "' + os.getcwd() + '"')
print_log('Main', 'Checking for data folder')
if not os.path.exists(config['UPLOAD_FOLDER']):
  print_log('Main', 'Data folder not found, creating')
  os.makedirs(config['UPLOAD_FOLDER'])
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)


def cleaner_thread():
  # Call itself again after the interval
  cleaner = Timer(config["CLEAN_INTERVAL"], cleaner_thread)
  cleaner.daemon = True # Daemons will attempt to exit cleanly along with the main process, which we want
  cleaner.start()

  # Actual function
Example #10
0
import json
import time
from random import randint

# Import our configuration
from conf import config

# Import QuadFile stuff
from QuadFile import db
from QuadFile.output import print_log, time_to_string
from QuadFile import application

app = Flask(__name__)

# TODO: Try to turn these into functions or something I dunno
print_log('Main', 'Running in "' + os.getcwd() + '"')
print_log('Main', 'Checking for data folder')
if not os.path.exists(config['UPLOAD_FOLDER']):
    print_log('Main', 'Data folder not found, creating')
    os.makedirs(config['UPLOAD_FOLDER'])
if config["EXTENDED_DEBUG"] == False:
    log = logging.getLogger('werkzeug')
    log.setLevel(logging.ERROR)


def cleaner_thread():
    # Call itself again after the interval
    cleaner = Timer(config["CLEAN_INTERVAL"], cleaner_thread)
    cleaner.daemon = True  # Daemons will attempt to exit cleanly along with the main process, which we want
    cleaner.start()
Example #11
0
def upload_file():
  if request.method == 'POST':
    print_log('Main', 'New file received', print_debug)
    if not application.basicauth(request.headers.get('X-Hyozan-Auth'), config["KEY"]):
      abort(403)
    data = dict()
    file = request.files['file']

    # Only continue if a file that's allowed gets submitted.
    if file and allowed_file(file):
      filename = secure_filename(file.filename)
      if filename.find(".")!=-1: #check if filename has a .(to check if it should split ext)
        filename = os.urandom(5).hex() + '.' + filename.rsplit('.',1)[1]
      else:
        filename = os.urandom(5).hex()

      thread1 = Thread(target = db.add_file, args = (filename,))
      thread1.start()
      print_log('Thread', 'Adding file to DB', print_debug)
      file.save(os.path.join(config['UPLOAD_FOLDER'], filename))
      thread1.join()

      data["file"] = filename
      data["url"] = config["DOMAIN"] + filename
      print_log('Main', 'New file processed "' + filename + '"')

      try:
        if request.form["source"] == "web":
          print_log('Web', 'Returned link page for "' + filename + '"', print_debug)
          return render_template('link.html', data=data, page=config["SITE_DATA"])
      except Exception:
        print_log('Web', 'No web reported in form, returned JSON', print_debug)
        return json.dumps(data)
    else:
      print_log('Notice', 'Forbidden file received')
      return error_page(error="This file isn't allowed, sorry!", code=403)

  # Return Web UI if we have a GET request
  elif request.method == 'GET':
    print_log('Web', 'Hit upload page')
    return render_template('upload.html', page=config["SITE_DATA"])
Example #12
0
import magic
from random import randint

# Import our configuration
from conf import config

# Import QuadFile stuff
from QuadFile import db
from QuadFile.output import print_log, time_to_string
from QuadFile import application

app = Flask(__name__)


# TODO: Try to turn these into functions or something I dunno
print_log('Main', 'Running in "' + os.getcwd() + '"')
print_log('Main', 'Checking for data folder')
if not os.path.exists(config['UPLOAD_FOLDER']):
  print_log('Warning', 'Data folder not found, creating')
  os.makedirs(config['UPLOAD_FOLDER'])
if not os.path.exists('files.db'):
  print_log('Warning', 'Database not found, attempting to create')
  os.system('sqlite3 files.db < schema.sql')
  if not os.path.exists('files.db'):
    print_log('Warning', 'Could not create database. Is sqlite3 available?')
    quit()
  else:
    print_log('Notice', 'Database created')
if config["EXTENDED_DEBUG"] == False:
  log = logging.getLogger('werkzeug')
  log.setLevel(logging.ERROR)