-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
103 lines (92 loc) · 4.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
import streamlit as st
import github
import hashlib
import os
import base64
import json
import tools
import config
from os import path
def err(msg: str):
print(f"Help: set {msg} varialbe")
raise Exception(f"you forgot to set environvariable {msg}")
def resErr(err: int):
if err == 429:
st.error("We had too many upload today, try again later.")
else:
st.error(f"there were an error with status code of: {res.status_code}")
if __name__=="__main__":
token = os.environ.get("TOKEN") if os.environ.get("TOKEN") else err("TOKEN")
owner = os.environ.get("OWNER") if os.environ.get("OWNER") else err("OWNER")
repo = os.environ.get("REPO") if os.environ.get("REPO") else err("REPO")
pages_link = os.environ.get("PAGES") if os.environ.get("PAGES") else err("PAGES")
st.set_page_config(
page_title = config.PAGE_TITLE,
page_icon = config.FAVICON,
layout = config.LAYOUT,
initial_sidebar_state = config.INITIAL_SIDEBAR_STATE,
menu_items = config.MENU_ITEM,
)
st.write("> Discover a versatile platform for **file sharing**, **pasting text**, and **shortening URLs** effortlessly.")
tab_file, tab_text, tab_link = st.tabs(["File", "Text", "Link"])
with tab_file:
fli = st.file_uploader(label="Select File", accept_multiple_files=False, help="file size should be under 100MB")
st.info("Maximum file size is 100MB")
st.divider()
if fli is not None:
fli_ext = fli.name
fli_ext = fli_ext.split('.')[-1]
fli = fli.getvalue()
if (len(fli)/(2**20)) < 100:
fli_name = hashlib.md5(fli).hexdigest() + '.' + fli_ext
fli = base64.b64encode(fli).decode()
res = github.upload(token, fli_name, fli, repo, owner, 'files')
if res.status_code == 201:
res = json.loads(res.content)
st.success("Done")
st.code(res['content']['download_url'])
elif res.status_code == 422:
st.warning("this file was exist before!")
st.code(f"https://raw.githubusercontent.com/{owner}/{repo}/master/files/{fli_name}")
else:
resErr(res.status_code)
else:
st.error("file size should be under 100MB")
with tab_text:
text = st.text_area("Paste your text here", placeholder="write your text here.", height=400)
if st.button("Paste"):
fli_name = tools.chooseName() + ".txt"
fli = base64.b64encode(bytes(text, encoding='utf-8')).decode()
res = github.upload(token, fli_name, fli, repo, owner)
if res.status_code == 201:
res = json.loads(res.content)
st.success("instanse link: ")
st.code(res['content']['download_url'])
st.divider()
st.warning("This link is shorten but you can use 1 minute later")
st.code(path.join(pages_link, fli_name))
elif res.status_code == 422:
st.warning("this file was exist before!")
st.code(f"https://raw.githubusercontent.com/{owner}/{repo}/master/files/{fli_name}")
st.divider()
else:
resErr(res.status_code)
with tab_link:
link = st.text_input(label="URL shortner", placeholder="place your URL here to be shorten")
if st.button("Make it Short"):
if tools.isValidateLink(link):
fli_name = tools.chooseName()
while fli_name in github.getContents(token, repo, owner):
fli_name = tools.chooseName()
fli = base64.b64encode(bytes(tools.makeShort(link), encoding='utf-8')).decode()
res = github.upload(token, fli_name+".html", fli, repo, owner)
if res.status_code != 201:
resErr(res.status_code)
res = json.loads(res.content)
st.code(pages_link+"/"+fli_name)
st.warning("it may take some minute to deploy the link")
else:
st.error("URL is invalid")
#TODO: use os.path.join()
st.warning(f"> Note that all your file will publicly avilable on https://github.com/{owner}/{repo}")