This commit is contained in:
edgar 2018-07-12 22:41:45 +03:00
commit 21f9030e27
15 changed files with 431 additions and 0 deletions

181
.gitignore vendored Normal file
View File

@ -0,0 +1,181 @@
# Created by https://www.gitignore.io/api/python,pycharm
### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
### PyCharm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
# Sonarlint plugin
.idea/sonarlint
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
### Python Patch ###
.venv/
# 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>

2
README.md Normal file
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)

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>