From 2f063811fc50b91d1ea86fa015f5ca791ea9fc74 Mon Sep 17 00:00:00 2001 From: 7415963987456321 Date: Tue, 19 Oct 2021 00:24:27 +0000 Subject: [PATCH 1/3] Add URLEncoder class --- fhost.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/fhost.py b/fhost.py index 8644ade..501ce76 100755 --- a/fhost.py +++ b/fhost.py @@ -29,7 +29,6 @@ from magic import Magic from mimetypes import guess_extension import sys import requests -from short_url import UrlEncoder from validators import url as url_valid from pathlib import Path @@ -91,8 +90,6 @@ Please install python-magic.""") db = SQLAlchemy(app) migrate = Migrate(app, db) -su = UrlEncoder(alphabet=app.config["URL_ALPHABET"], block_size=16) - class URL(db.Model): id = db.Column(db.Integer, primary_key = True) url = db.Column(db.UnicodeText, unique = True) @@ -207,6 +204,30 @@ class File(db.Model): db.session.commit() return f + + +class UrlEncoder(object): + def __init__(self,alphabet): + self.alphabet = alphabet + + def enbase(self, x, min_length): + n = len(self.alphabet) + str = "" + while x > 0: + str = (self.alphabet[int(x % n)]) + str + x = int(x // n) + padding = self.alphabet[0] * (min_length - len(str)) + return '%s%s' % (padding, str) + + def debase(self, x): + n = len(self.alphabet) + result = 0 + for i, c in enumerate(reversed(x)): + result += self.alphabet.index(c) * (n ** i) + return result + +su = UrlEncoder(alphabet=app.config["URL_ALPHABET"]) + def fhost_url(scheme=None): if not scheme: return url_for(".fhost", _external=True).rstrip("/") -- 2.47.0 From 67287ab45192d2f17bb9e8fc6a9e1e7e9f359a3b Mon Sep 17 00:00:00 2001 From: 7415963987456321 Date: Tue, 19 Oct 2021 00:39:09 +0000 Subject: [PATCH 2/3] Refactor min_length of urls --- fhost.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fhost.py b/fhost.py index 501ce76..c70ed17 100755 --- a/fhost.py +++ b/fhost.py @@ -98,7 +98,7 @@ class URL(db.Model): self.url = url def getname(self): - return su.enbase(self.id, 1) + return su.enbase(self.id) def geturl(self): return url_for("get", path=self.getname(), _external=True) + "\n" @@ -129,7 +129,7 @@ class File(db.Model): self.addr = addr def getname(self): - return u"{0}{1}".format(su.enbase(self.id, 1), self.ext) + return u"{0}{1}".format(su.enbase(self.id), self.ext) def geturl(self): n = self.getname() @@ -207,16 +207,17 @@ class File(db.Model): class UrlEncoder(object): - def __init__(self,alphabet): + def __init__(self,alphabet, min_length): self.alphabet = alphabet + self.min_length = min_length - def enbase(self, x, min_length): + def enbase(self, x): n = len(self.alphabet) str = "" while x > 0: str = (self.alphabet[int(x % n)]) + str x = int(x // n) - padding = self.alphabet[0] * (min_length - len(str)) + padding = self.alphabet[0] * (self.min_length - len(str)) return '%s%s' % (padding, str) def debase(self, x): @@ -226,7 +227,7 @@ class UrlEncoder(object): result += self.alphabet.index(c) * (n ** i) return result -su = UrlEncoder(alphabet=app.config["URL_ALPHABET"]) +su = UrlEncoder(alphabet=app.config["URL_ALPHABET"], min_length=1) def fhost_url(scheme=None): if not scheme: -- 2.47.0 From 402a560e167755216a98cd75c521a44c29c20b3b Mon Sep 17 00:00:00 2001 From: 7415963987456321 Date: Tue, 19 Oct 2021 00:51:20 +0000 Subject: [PATCH 3/3] Forgot to remove dependency --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index aa059d9..8dbfe72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,3 @@ Flask_SQLAlchemy validators flask_migrate python_magic -short_url -- 2.47.0