Exemplo n.º 1
0
def store(fp):
    realFilename = fp.filename
    md5sum = _hash_file(fp, hashlib.md5())
    sha1sum = _hash_file(fp, hashlib.sha1())
    fp.seek(0)

    fileData = model.File.query.filter(model.File.MD5Sum == md5sum,
                                       model.File.SHA1Sum == sha1sum).first()

    if not fileData:
        while True:
            newFilename = common.generate_random_string(32)
            if not os.path.exists(
                    os.path.join(app.config['UPLOAD_FULL_DIRECTORY'],
                                 newFilename)):
                break
        fullPath = os.path.join(app.config['UPLOAD_FULL_DIRECTORY'],
                                newFilename)
        fp.save(fullPath)
        fileSize = os.stat(fullPath).st_size
        fileData = model.File(
            os.path.join(app.config["UPLOAD_DIRECTORY"], newFilename), md5sum,
            sha1sum, fileSize)
        db.session.add(fileData)
        db.session.commit()

    return fileData
Exemplo n.º 2
0
    def test_all(self):
        n = 10
        initial_keys = [generate_random_string() for _ in range(n)]
        more_keys = [generate_random_string() for _ in range(n // 3)]
        myhashes, mykeys = create_new(initial_keys)

        for key in more_keys:
            insert(myhashes, mykeys, key)
            insert(myhashes, mykeys, key)

        existing_keys = initial_keys + more_keys
        for key in existing_keys:
            self.assertTrue(has_key(myhashes, mykeys, key))

        myhashes, mykeys = resize(myhashes, mykeys)

        for key in existing_keys:
            self.assertTrue(has_key(myhashes, mykeys, key))

        missing_keys = [generate_random_string() for _ in range(3 * n)]
        for key in set(missing_keys) - set(existing_keys):
            self.assertFalse(has_key(myhashes, mykeys, key))
            with self.assertRaises(KeyError):
                remove(myhashes, mykeys, key)

        for key in existing_keys:
            self.assertTrue(has_key(myhashes, mykeys, key))
            remove(myhashes, mykeys, key)
            self.assertFalse(has_key(myhashes, mykeys, key))

        for key in more_keys:
            self.assertFalse(has_key(myhashes, mykeys, key))
            insert(myhashes, mykeys, key)
            self.assertTrue(has_key(myhashes, mykeys, key))
            remove(myhashes, mykeys, key)
            self.assertFalse(has_key(myhashes, mykeys, key))
Exemplo n.º 3
0
def create_path(fileNo, fileName, method="Web", optExpiresIn=None, optDownloadLimit=None, optHideAfterLimitExceeded=None, optGroup=None):
	pathLength = 3 # default
	while True:
		try:
			newPath = Path(common.generate_random_string(int(pathLength)), fileNo,
				fileName, int(time.time()), method, request.remote_addr, optExpiresIn, optDownloadLimit,
				optHideAfterLimitExceeded, optGroup)
			db.session.add(newPath)
			db.session.commit()
			break
		except IntegrityError:
			print traceback.format_exc()
			pathLength += 0.2 # increase length every five attempts
			db.session.rollback()

	return newPath
Exemplo n.º 4
0
def create_group():
	if request.method == "POST":
		if "group_path" in request.form:
			groupPath = request.form["group_path"]
			try:
				db.session.add(model.Group(groupPath,
					request.form.get("description", "")))
				db.session.commit()
			except IntegrityError:
				db.session.rollback()
				return json.dumps({"result": False, "error": "Already exists"})

		else:
			pathLength = 3 # default
			while True:
				try:
					groupPath = common.generate_random_string(int(pathLength))
					db.session.add(model.Group(groupPath,
						request.form.get("description", "")))
					db.session.commit()
					break
				except IntegrityError:
					db.session.rollback()
					pathLength += 0.2

		result = {}
		for p in request.form.get("paths", "").split(","):
			p = p.strip()
			if not p:
				continue
			fileData = model.Path.query.filter(model.Path.Path == p).first()

			if not fileData:
				result.update({p: False})

			fileData.Group = groupPath

			db.session.commit()

			result.update({p: True})

		db.session.commit()

		return json.dumps({"path": groupPath, "result": result})

	return render_template("groups_create.html")
Exemplo n.º 5
0
def store(fp):
	realFilename = fp.filename
	md5sum = _hash_file(fp, hashlib.md5())
	sha1sum = _hash_file(fp, hashlib.sha1())
	fp.seek(0)

	fileData = model.File.query.filter(model.File.MD5Sum == md5sum,
		model.File.SHA1Sum == sha1sum).first()

	if not fileData:
		while True:
			newFilename = common.generate_random_string(32)
			if not os.path.exists(os.path.join(app.config['UPLOAD_FULL_DIRECTORY'], newFilename)):
				break
		fullPath = os.path.join(app.config['UPLOAD_FULL_DIRECTORY'], newFilename)
		fp.save(fullPath)
		fileSize = os.stat(fullPath).st_size
		fileData = model.File(os.path.join(app.config["UPLOAD_DIRECTORY"], newFilename),
			md5sum, sha1sum, fileSize)
		db.session.add(fileData)
		db.session.commit()

	return fileData
Exemplo n.º 6
0
def create_path(fileNo,
                fileName,
                method="Web",
                optExpiresIn=None,
                optDownloadLimit=None,
                optHideAfterLimitExceeded=None,
                optGroup=None):
    pathLength = 3  # default
    while True:
        try:
            newPath = Path(common.generate_random_string(int(pathLength)),
                           fileNo, fileName, int(time.time()), method,
                           request.remote_addr, optExpiresIn, optDownloadLimit,
                           optHideAfterLimitExceeded, optGroup)
            db.session.add(newPath)
            db.session.commit()
            break
        except IntegrityError:
            print traceback.format_exc()
            pathLength += 0.2  # increase length every five attempts
            db.session.rollback()

    return newPath
Exemplo n.º 7
0
def group_zip(path, groupData):
	if not app.config.get("ENABLE_API", True) and not app.config.get("ENABLE_ZIP", False):
		return abort(404)

	for fileData in groupData.Paths:
		if (fileData.DownloadLimit is not None and fileData.Downloaded >= fileData.DownloadLimit) or \
			(fileData.ExpiresIn is not None and time.time() > fileData.Uploaded + fileData.ExpiresIn):
			db.session.rollback()
			return render_template("limit_exceeded.html")
		else:
			fileData.Downloaded += 1
			db.session.commit()

	db.session.commit()

	zPath = os.path.join("/tmp", common.generate_random_string(32))
	zFp = zipfile.ZipFile(zPath, "w", app.config.get("ZIP_METHOD", zipfile.ZIP_DEFLATED))
	for fileData in groupData.Paths:
		zFp.write(os.path.join(app.config["UPLOAD_BASE_DIR"], fileData.File.StoredPath), fileData.ActualName)
	zFp.close()
	response = make_response(send_file(zPath))
	response.headers["Content-Disposition"] = "attachment; filename=\"%s.zip\""%(path)

	return response
Exemplo n.º 8
0
saTables = saInspector.get_table_names()

migrate = Migrate(app, db)

with app.app_context() as c:
	if "Config" not in saTables and "Path" not in saTables:
		print "-"*100
		print "Initializing..."

		print "Creating tables..."
#		db.create_all()
		upgrade()

		print "Created tables successfully"

		tmpPW = common.generate_random_string(8)

		from flask.ext.bcrypt import generate_password_hash, check_password_hash

		print "Creating default user..."
		db.session.add(User("admin", generate_password_hash(tmpPW), common.generate_random_string(32)))
		db.session.commit()
		print "Created new user: admin / %s"%(tmpPW)

		print "-"*100

	elif "Config" not in saTables:
		# Temporary patch for those who are using depot
		# version earlier than commit c0a0e1d
		stamp(revision="710d5081fa7")
		upgrade()
Exemplo n.º 9
0
saTables = saInspector.get_table_names()

migrate = Migrate(app, db)

with app.app_context() as c:
    if "Config" not in saTables and "Path" not in saTables:
        print "-" * 100
        print "Initializing..."

        print "Creating tables..."
        #		db.create_all()
        upgrade()

        print "Created tables successfully"

        tmpPW = common.generate_random_string(8)

        from flask.ext.bcrypt import generate_password_hash, check_password_hash

        print "Creating default user..."
        db.session.add(
            User("admin", generate_password_hash(tmpPW),
                 common.generate_random_string(32)))
        db.session.commit()
        print "Created new user: admin / %s" % (tmpPW)

        print "-" * 100

    elif "Config" not in saTables:
        # Temporary patch for those who are using depot
        # version earlier than commit c0a0e1d
Exemplo n.º 10
0
def signup():
	if not app.config.get("ENABLE_SIGNUP", False):
		return abort(404)

	elif (request.method == "POST" and
		request.form["id"] and request.form["password"]):
		u = model.User(request.form["id"], generate_password_hash(request.form["password"]), common.generate_random_string(32))
		db.session.add(u)
		db.session.commit()
		return redirect(url_for("signin"))
	else:
		return render_template("signup.html")
Exemplo n.º 11
0
def api_regenerate_key():
	uinfo = load_user(session["user_id"])
	uinfo.APIKey = common.generate_random_string(32)
	db.session.commit()
	return redirect(url_for("overview"))