diff --git a/.gitignore b/.gitignore index 31468fb..4f3eb8b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,5 @@ tests/ logo/ app.json Procfile -.vscode +.vscode/ .gitignore \ No newline at end of file diff --git a/app/config.py b/app/config.py index 67e9a76..a513e53 100644 --- a/app/config.py +++ b/app/config.py @@ -8,7 +8,8 @@ import os try: port = int(os.environ.get("PORT", "8080")) -except ValueError: +except Exception as e: + print(e) port = -1 if not 1 <= port <= 65535: print( diff --git a/app/routes.py b/app/routes.py index e0b028d..487fd88 100644 --- a/app/routes.py +++ b/app/routes.py @@ -30,6 +30,8 @@ async def setup_routes(app, handler): web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), + web.get(p + r"/{id:\d+}/v.mp4", h.download_get), + web.head(p + r"/{id:\d+}/v.mp4", h.download_head), ] if index_all: # print(await client.get_dialogs()) diff --git a/app/templates/header.html b/app/templates/header.html index 06a89ac..ead6e53 100644 --- a/app/templates/header.html +++ b/app/templates/header.html @@ -7,6 +7,24 @@ + + + + +
- {{item_list|length}} items -
- -Page {{cur_page}}
- {% if next_page %} - Page {{next_page.no}} - {% endif %} -- No message to display! -
- + ++ {{item_list|length}} items +
+ ++ Page {{cur_page}}
+ {% if next_page %} + Page {{next_page.no}} + {% endif %} ++ No message to display! +
+ +{% endif %} + +{% include 'footer.html' %} \ No newline at end of file diff --git a/app/templates/js/filesaver.min.js b/app/templates/js/filesaver.min.js new file mode 100644 index 0000000..a6f0e37 --- /dev/null +++ b/app/templates/js/filesaver.min.js @@ -0,0 +1,2 @@ +//# sourceMappingURL=FileSaver.min.js.map +(function(a,b){if("function"==typeof define&&define.amd)define([],b);else if("undefined"!=typeof exports)b();else{b(),a.FileSaver={exports:{}}.exports}})(this,function(){"use strict";function b(a,b){return"undefined"==typeof b?b={autoBom:!1}:"object"!=typeof b&&(console.warn("Deprecated: Expected third argument to be a object"),b={autoBom:!b}),b.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(a.type)?new Blob(["\uFEFF",a],{type:a.type}):a}function c(a,b,c){var d=new XMLHttpRequest;d.open("GET",a),d.responseType="blob",d.onload=function(){g(d.response,b,c)},d.onerror=function(){console.error("could not download file")},d.send()}function d(a){var b=new XMLHttpRequest;b.open("HEAD",a,!1);try{b.send()}catch(a){}return 200<=b.status&&299>=b.status}function e(a){try{a.dispatchEvent(new MouseEvent("click"))}catch(c){var b=document.createEvent("MouseEvents");b.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),a.dispatchEvent(b)}}var f="object"==typeof window&&window.window===window?window:"object"==typeof self&&self.self===self?self:"object"==typeof global&&global.global===global?global:void 0,a=/Macintosh/.test(navigator.userAgent)&&/AppleWebKit/.test(navigator.userAgent)&&!/Safari/.test(navigator.userAgent),g=f.saveAs||("object"!=typeof window||window!==f?function(){}:"download"in HTMLAnchorElement.prototype&&!a?function(b,g,h){var i=f.URL||f.webkitURL,j=document.createElement("a");g=g||b.name||"download",j.download=g,j.rel="noopener","string"==typeof b?(j.href=b,j.origin===location.origin?e(j):d(j.href)?c(b,g,h):e(j,j.target="_blank")):(j.href=i.createObjectURL(b),setTimeout(function(){i.revokeObjectURL(j.href)},4E4),setTimeout(function(){e(j)},0))}:"msSaveOrOpenBlob"in navigator?function(f,g,h){if(g=g||f.name||"download","string"!=typeof f)navigator.msSaveOrOpenBlob(b(f,h),g);else if(d(f))c(f,g,h);else{var i=document.createElement("a");i.href=f,i.target="_blank",setTimeout(function(){e(i)})}}:function(b,d,e,g){if(g=g||open("","_blank"),g&&(g.document.title=g.document.body.innerText="downloading..."),"string"==typeof b)return c(b,d,e);var h="application/octet-stream"===b.type,i=/constructor/i.test(f.HTMLElement)||f.safari,j=/CriOS\/[\d]+/.test(navigator.userAgent);if((j||h&&i||a)&&"undefined"!=typeof FileReader){var k=new FileReader;k.onloadend=function(){var a=k.result;a=j?a:a.replace(/^data:[^;]*;/,"data:attachment/file;"),g?g.location.href=a:location=a,g=null},k.readAsDataURL(b)}else{var l=f.URL||f.webkitURL,m=l.createObjectURL(b);g?g.location=m:location.href=m,g=null,setTimeout(function(){l.revokeObjectURL(m)},4E4)}});f.saveAs=g.saveAs=g,"undefined"!=typeof module&&(module.exports=g)}); diff --git a/app/templates/js/playlist.js b/app/templates/js/playlist.js new file mode 100644 index 0000000..ff73986 --- /dev/null +++ b/app/templates/js/playlist.js @@ -0,0 +1,26 @@ +function singleItemPlaylist(file,name){ + let hostUrl = 'http://' + window.location.host + let pd = "" + pd += '#EXTM3U\n' + pd += `#EXTINF: ${name}\n` + pd += `${hostUrl}/${file}\n` + let blob = new Blob([pd], { endings: "native" }); + saveAs(blob, `${name}.m3u`); +} + +// function createPlaylist(indexSite, id, playlistName = "Playlist", duration = 60) { +// let pd = "" +// name = playlistName +// pd += '#EXTM3U\n' +// pd += `#EXTINF: ${duration * 60} | ${name}\n` +// pd += `${indexSite}/${id}/v.mp4\n` +// return pd +// } + +// function playlist(id, name) { +// hostUrl = 'https://' + window.location.host +// playlistData = createPlaylist(hostUrl, id, name); +// let blob = new Blob([playlistData], { endings: "native" }); +// saveAs(blob, `${name}.m3u`); +// } + diff --git a/app/views/__init__.py b/app/views/__init__.py index a8ed5c3..538848e 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -31,24 +31,23 @@ class Views( ): def __init__(self, client): self.client = client - + self.url_len = SHORT_URL_LEN self.chat_ids = {} def generate_alias_id(self, chat): chat_id = chat.id title = chat.title - while True: - # alias_id = "".join( - # [ - # random.choice(string.ascii_letters + string.digits) - # for _ in range(len(str(chat_id))) - # ] - # ) - orig_id = f"{title}{chat_id}" # the original id - alias_id = base64.urlsafe_b64encode(hashlib.md5(orig_id.encode()).digest())[:SHORT_URL_LEN].decode() + while True: + orig_id = f"{chat_id}" # the original id + unique_hash = hashlib.md5(orig_id.encode()).digest() + alias_id = base64.urlsafe_b64encode(unique_hash).decode()[:self.url_len] + if alias_id in self.chat_ids: + self.url_len += 1 # increment url_len just incase the hash is already used. continue + elif (self.url_len > SHORT_URL_LEN): # reset url_len to initial if hash was unique. + self.url_len = SHORT_URL_LEN self.chat_ids[alias_id] = { "chat_id": chat_id, diff --git a/app/views/index_view.py b/app/views/index_view.py index 0d46e1d..faa7fbb 100644 --- a/app/views/index_view.py +++ b/app/views/index_view.py @@ -59,6 +59,8 @@ class IndexView: insight=get_file_name(m), human_size=get_human_size(m.file.size), url=f"/{alias_id}/{m.id}/view", + download=f"{alias_id}/{m.id}/download", + vlc = f"{alias_id}/{m.id}/v.mp4", ) elif m.message: entry = dict( diff --git a/app/views/thumbnail_view.py b/app/views/thumbnail_view.py index 16f819a..737c9f6 100644 --- a/app/views/thumbnail_view.py +++ b/app/views/thumbnail_view.py @@ -46,7 +46,12 @@ class ThumbnailView: body = temp.getvalue() else: thumb_pos = int(len(thumbnails) / 2) - thumbnail = self.client._get_thumb(thumbnails, thumb_pos) + try: + thumbnail = self.client._get_thumb(thumbnails, thumb_pos) + except Exception as e: + logging.debug(e) + thumbnail = None + if not thumbnail or isinstance(thumbnail, types.PhotoSizeEmpty): return web.Response( status=410,