Add URLEncoder class
This commit is contained in:
parent
47ff3a1152
commit
2f063811fc
1 changed files with 24 additions and 3 deletions
27
fhost.py
27
fhost.py
|
@ -29,7 +29,6 @@ from magic import Magic
|
||||||
from mimetypes import guess_extension
|
from mimetypes import guess_extension
|
||||||
import sys
|
import sys
|
||||||
import requests
|
import requests
|
||||||
from short_url import UrlEncoder
|
|
||||||
from validators import url as url_valid
|
from validators import url as url_valid
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ -91,8 +90,6 @@ Please install python-magic.""")
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
migrate = Migrate(app, db)
|
migrate = Migrate(app, db)
|
||||||
|
|
||||||
su = UrlEncoder(alphabet=app.config["URL_ALPHABET"], block_size=16)
|
|
||||||
|
|
||||||
class URL(db.Model):
|
class URL(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key = True)
|
id = db.Column(db.Integer, primary_key = True)
|
||||||
url = db.Column(db.UnicodeText, unique = True)
|
url = db.Column(db.UnicodeText, unique = True)
|
||||||
|
@ -207,6 +204,30 @@ class File(db.Model):
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return f
|
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):
|
def fhost_url(scheme=None):
|
||||||
if not scheme:
|
if not scheme:
|
||||||
return url_for(".fhost", _external=True).rstrip("/")
|
return url_for(".fhost", _external=True).rstrip("/")
|
||||||
|
|
Loading…
Reference in a new issue