summaryrefslogtreecommitdiff
path: root/fhost.py
diff options
context:
space:
mode:
authormia <mia@0x0.st>2021-12-01 13:25:33 +0100
committermia <mia@0x0.st>2021-12-01 13:25:33 +0100
commit9c4a0fd5a6de5a1b15444171559e9dce0a2fa578 (patch)
tree3b84616512ee8410631d860b178220d68a7470c2 /fhost.py
parentb8def71a94bd516d1b3bc684e0db704a2d177d7a (diff)
remove short_url and add in-tree URLencoder (#53)
This PR removes the short_url dependency as per issue #41. This implementation is pretty much the same as in short_url except I've rewritten the enbase() function to be iterative instead of recursive. The only functions of the class are enbase() and debase() since those were the only functions being used by fhost. Co-authored-by: 7415963987456321 <hrs70@hi.is> Reviewed-on: https://git.0x0.st/mia/0x0/pulls/53 Co-authored-by: mia <mia@0x0.st> Co-committed-by: mia <mia@0x0.st>
Diffstat (limited to 'fhost.py')
-rwxr-xr-xfhost.py32
1 files changed, 27 insertions, 5 deletions
diff --git a/fhost.py b/fhost.py
index 8644ade..c70ed17 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)
@@ -101,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"
@@ -132,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,6 +204,31 @@ class File(db.Model):
db.session.commit()
return f
+
+
+class UrlEncoder(object):
+ def __init__(self,alphabet, min_length):
+ self.alphabet = alphabet
+ self.min_length = 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] * (self.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"], min_length=1)
+
def fhost_url(scheme=None):
if not scheme:
return url_for(".fhost", _external=True).rstrip("/")