Compare commits

...

No commits in common. "857ceb3106ea7d31a29b41dae77e7759b3b57169" and "21f9030e275d8d3700f4ae94d66649736638b68b" have entirely different histories.

18 changed files with 250 additions and 29 deletions

1
.gitignore vendored
View File

@ -179,4 +179,3 @@ venv.bak/
# End of https://www.gitignore.io/api/python,pycharm
img/

24
.idea/imgshare.iml generated Normal file
View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="Flask">
<option name="enabled" value="true" />
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
<option name="TEMPLATE_FOLDERS">
<list>
<option value="$MODULE_DIR$/templates" />
</list>
</option>
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>

7
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6 (imgshare)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/imgshare.iml" filepath="$PROJECT_DIR$/.idea/imgshare.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,2 @@
# Imageshare Server

102
app.py Normal file
View File

@ -0,0 +1,102 @@
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, abort
import os
import random
import string
app = Flask(__name__)
IMAGE_FOLDER = os.path.basename('img')
ALLOWED_EXTENSIONS = {'jpg', 'png', 'gif', 'jpeg', 'webm', 'gifv', 'mp4'}
SERVER_URL = 'http://localhost:5000'
app.config['FILE_FOLDER'] = IMAGE_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024
if not os.path.exists(IMAGE_FOLDER):
os.mkdir(IMAGE_FOLDER)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
print(request)
if 'file' not in request.files:
print("no image found")
return abort(400)
file = request.files['file']
if file.filename == '':
return abort(400)
if file:
file_extension = get_file_extension(file.filename)
if file_extension:
random_unique_filename = generate_random_filename(file_extension)
file.save(os.path.join(app.config['FILE_FOLDER'], random_unique_filename))
url = url_for('get_file', filename=random_unique_filename)
print(url)
return redirect(url)
else:
return abort(415)
else:
return abort(400)
@app.route('/uploadcli', methods=['POST'])
def uploadcli_file():
print(request)
if 'file' not in request.files:
print("no image found")
return abort(400)
file = request.files['file']
if file.filename == '':
return abort(400)
if file:
file_extension = get_file_extension(file.filename)
if file_extension:
random_unique_filename = generate_random_filename(file_extension)
file.save(os.path.join(app.config['FILE_FOLDER'], random_unique_filename))
url = url_for('get_file', filename=random_unique_filename)
print(url)
return SERVER_URL + url
else:
return abort(415)
else:
return abort(400)
@app.route('/<filename>')
def get_file(filename):
url = app.config['FILE_FOLDER']
if url:
return send_from_directory(url, filename)
else:
return abort(404)
def get_file_extension(filename):
try:
extension = filename.rsplit('.', 1)[1].lower()
if extension in ALLOWED_EXTENSIONS:
return extension
else:
return ''
except:
return ''
def generate_random_filename(file_extension):
while True:
filename = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + '.' + file_extension
if not os.path.exists(os.path.join(app.config['FILE_FOLDER'], filename)):
return filename
if __name__ == '__main__':
app.run('127.0.0.1', 5000)

View File

@ -1,13 +0,0 @@
import requests
import sys
import pyperclip
URL = 'http://localhost:5000/uploadcli'
file = {'file': open(sys.argv[1], 'rb')}
r = requests.post(URL, files=file)
if r.status_code == 200:
pyperclip.copy(r.text)
else:
print(r.text)

View File

@ -1,9 +0,0 @@
#!/usr/bin/env bash
DATE=`date '+%Y-%m-%d-%H-%M-%S'`
FULL_PATH="/home/USER/Pictures/Screenshots/"$DATE".jpg"
echo $FULL_PATH
scrot -s "$FULL_PATH"
python client.py $FULL_PATH

View File

@ -1,6 +0,0 @@
{
"Name": "imageshare",
"DestinationType": "ImageUploader",
"RequestURL": "localhost:5000/uploadcli",
"FileFormName": "file"
}

7
static/css/bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

7
static/js/bootstrap.bundle.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

7
static/js/bootstrap.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

27
static/js/custom.js Normal file
View File

@ -0,0 +1,27 @@
$(document).ready(function(){
$('#upload-file-btn').click(function(){
var input = document.querySelector('input[type="file"]');
var data = new FormData();
data.append('file', input.files[0]);
fetch('/upload', {
method: 'POST',
body: data,
})
.then(function(data){
switch(data.status) {
case 200:
window.location = data.url;
break;
case 400:
console.log("Upload failed");
break;
case 415:
console.log("Wrong file format");
break;
}
});
});
});

2
static/js/jquery.min.js vendored Normal file

File diff suppressed because one or more lines are too long

54
templates/index.html Normal file
View File

@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Index</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/bootstrap.min.css') }}">
<style>
body {
padding-top: 54px;
}
@media (min-width: 992px) {
body {
padding-top: 56px;
}
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="#">Imageshare</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h1 class="mt-5">Upload your picture or video</h1>
<form id="upload-file" method="post" enctype="multipart/form-data">
<fieldset>
<input name="file" class="form-control input-lg" type="file" accept=".jpg, .png, .jpeg, .gif, .webm, .gifv, .mp4">
</fieldset>
<fieldset>
<button id="upload-file-btn" type="button">Upload</button>
</fieldset>
</form>
</div>
</div>
</div>
<script type="text/javascript" src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script type="text/javascript" src="{{ url_for('static', filename='js/custom.js') }}"></script>
</body>
</html>