infra: deploy script + webhook server + S3 uploader (saguaro test)
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
#!/usr/bin/env python3
|
||||
import os, tempfile
|
||||
from flask import Flask, request, jsonify
|
||||
import boto3
|
||||
from botocore.client import Config
|
||||
from botocore.exceptions import ClientError
|
||||
import pillow_heif
|
||||
from PIL import Image
|
||||
pillow_heif.register_heif_opener()
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50MB
|
||||
|
||||
S3_CONFIG = dict(
|
||||
endpoint_url='https://s3.regru.cloud',
|
||||
aws_access_key_id='TXBN3MRO1YCC2Y593URD',
|
||||
aws_secret_access_key='GIMA9lJetvXgMtl8GCO4dNSpJDGHvKgGBVfiA9zV',
|
||||
region_name='ru-1',
|
||||
config=Config(signature_version='s3v4')
|
||||
)
|
||||
BUCKET = 'sleeptrip-dev'
|
||||
PREFIX = 'images/'
|
||||
ALLOWED = {'jpg', 'jpeg', 'png', 'gif', 'webp', 'mp4', 'mov', 'heic', 'heif'}
|
||||
MIME = {
|
||||
'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'png': 'image/png',
|
||||
'gif': 'image/gif', 'webp': 'image/webp',
|
||||
'mp4': 'video/mp4', 'mov': 'video/quicktime',
|
||||
'heic': 'image/jpeg', 'heif': 'image/jpeg'
|
||||
}
|
||||
|
||||
PAGE = """<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Upload to S3</title>
|
||||
<style>
|
||||
*{box-sizing:border-box;margin:0;padding:0}
|
||||
body{background:#0d1117;color:#e6edf3;font-family:-apple-system,BlinkMacSystemFont,monospace;display:flex;align-items:center;justify-content:center;min-height:100vh;padding:24px}
|
||||
.card{background:#161b22;border:1px solid #30363d;border-radius:12px;padding:32px;width:100%;max-width:520px}
|
||||
h1{font-size:16px;font-weight:600;margin-bottom:4px}
|
||||
.sub{font-size:12px;color:#8b949e;margin-bottom:24px}
|
||||
.drop{border:2px dashed #30363d;border-radius:8px;padding:48px 24px;text-align:center;cursor:pointer;transition:all .2s}
|
||||
.drop.over{border-color:#58a6ff;background:rgba(88,166,255,.05)}
|
||||
.drop input{display:none}
|
||||
.drop-icon{font-size:36px;margin-bottom:12px}
|
||||
.drop-text{font-size:13px;color:#8b949e}
|
||||
.drop-text b{color:#58a6ff;cursor:pointer}
|
||||
.progress{margin-top:16px;display:none}
|
||||
.bar-wrap{background:#21262d;border-radius:4px;height:6px;margin-top:8px}
|
||||
.bar{background:#58a6ff;height:6px;border-radius:4px;width:0;transition:width .15s}
|
||||
.status{font-size:12px;color:#8b949e;margin-top:6px}
|
||||
.result{margin-top:20px;display:none}
|
||||
.url-box{background:#0d1117;border:1px solid #3fb950;border-radius:6px;padding:12px 14px;font-size:12px;font-family:monospace;word-break:break-all;color:#3fb950;cursor:pointer}
|
||||
.url-box:hover{background:rgba(63,185,80,.05)}
|
||||
.copy-hint{font-size:11px;color:#8b949e;margin-top:6px}
|
||||
.preview{margin-top:12px;max-width:100%;max-height:200px;border-radius:6px;display:none}
|
||||
.err{color:#f85149;font-size:12px;margin-top:12px;display:none;padding:8px 12px;background:rgba(248,81,73,.08);border-radius:6px}
|
||||
.hist{margin-top:24px;border-top:1px solid #21262d;padding-top:16px;display:none}
|
||||
.hist h2{font-size:11px;text-transform:uppercase;letter-spacing:1px;color:#8b949e;margin-bottom:10px}
|
||||
.hi{font-size:11px;font-family:monospace;color:#8b949e;padding:5px 0;border-bottom:1px solid #21262d;cursor:pointer;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||
.hi:hover{color:#58a6ff}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<h1>Upload → S3</h1>
|
||||
<p class="sub">sleeptrip-dev · s3.regru.cloud/images/</p>
|
||||
<div class="drop" id="drop">
|
||||
<input type="file" id="fi" accept="image/*,video/*" multiple>
|
||||
<div class="drop-icon">⇧</div>
|
||||
<div class="drop-text">Перетащи или <b id="btn">выбери файл</b></div>
|
||||
</div>
|
||||
<div class="progress" id="prog">
|
||||
<div class="bar-wrap"><div class="bar" id="bar"></div></div>
|
||||
<div class="status" id="stat">Загружаем...</div>
|
||||
</div>
|
||||
<div class="err" id="err"></div>
|
||||
<div class="result" id="res">
|
||||
<div class="url-box" id="url" onclick="copy(this)"></div>
|
||||
<div class="copy-hint" id="hint">Нажми чтобы скопировать</div>
|
||||
<img class="preview" id="prev">
|
||||
</div>
|
||||
<div class="hist" id="hist">
|
||||
<h2>История</h2>
|
||||
<div id="hl"></div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
var urls = [];
|
||||
var drop = document.getElementById('drop');
|
||||
var fi = document.getElementById('fi');
|
||||
document.getElementById('btn').onclick = function(){ fi.click(); };
|
||||
drop.addEventListener('dragover', function(e){ e.preventDefault(); drop.classList.add('over'); });
|
||||
drop.addEventListener('dragleave', function(){ drop.classList.remove('over'); });
|
||||
drop.addEventListener('drop', function(e){ e.preventDefault(); drop.classList.remove('over'); for(var i=0;i<e.dataTransfer.files.length;i++) doUpload(e.dataTransfer.files[i]); });
|
||||
fi.addEventListener('change', function(){ for(var i=0;i<fi.files.length;i++) doUpload(fi.files[i]); });
|
||||
|
||||
function doUpload(file) {
|
||||
var prog = document.getElementById('prog');
|
||||
var res = document.getElementById('res');
|
||||
var err = document.getElementById('err');
|
||||
prog.style.display = 'block';
|
||||
res.style.display = 'none';
|
||||
err.style.display = 'none';
|
||||
document.getElementById('bar').style.width = '0';
|
||||
document.getElementById('stat').textContent = file.name + ' ...';
|
||||
var fd = new FormData();
|
||||
fd.append('file', file);
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.upload.onprogress = function(e){ if(e.lengthComputable) document.getElementById('bar').style.width = (e.loaded/e.total*100)+'%'; };
|
||||
xhr.onload = function(){
|
||||
prog.style.display = 'none';
|
||||
if(xhr.status === 200){
|
||||
var r = JSON.parse(xhr.responseText);
|
||||
showRes(r.url, file.type.indexOf('image')===0);
|
||||
} else {
|
||||
var msg = 'Ошибка '+xhr.status;
|
||||
try{ msg = JSON.parse(xhr.responseText).error; }catch(e){}
|
||||
showErr(msg);
|
||||
}
|
||||
};
|
||||
xhr.onerror = function(){ prog.style.display='none'; showErr('Сетевая ошибка'); };
|
||||
xhr.open('POST', '/upload/api');
|
||||
xhr.withCredentials = true;
|
||||
xhr.send(fd);
|
||||
}
|
||||
|
||||
function showRes(url, isImg) {
|
||||
var el = document.getElementById('url');
|
||||
el.textContent = url;
|
||||
document.getElementById('res').style.display = 'block';
|
||||
document.getElementById('hint').textContent = 'Нажми чтобы скопировать';
|
||||
var p = document.getElementById('prev');
|
||||
if(isImg){ p.src=url; p.style.display='block'; } else { p.style.display='none'; }
|
||||
urls.unshift(url);
|
||||
updateHist();
|
||||
}
|
||||
|
||||
function showErr(msg){ var e=document.getElementById('err'); e.textContent=msg; e.style.display='block'; }
|
||||
|
||||
function copy(el){
|
||||
navigator.clipboard.writeText(el.textContent).then(function(){
|
||||
document.getElementById('hint').textContent='Скопировано!';
|
||||
setTimeout(function(){ document.getElementById('hint').textContent='Нажми чтобы скопировать'; }, 2000);
|
||||
});
|
||||
}
|
||||
|
||||
function updateHist(){
|
||||
document.getElementById('hist').style.display='block';
|
||||
var html = '';
|
||||
for(var i=0;i<urls.length;i++){
|
||||
html += '<div class="hi" data-url="'+urls[i]+'">'+urls[i]+'</div>';
|
||||
}
|
||||
document.getElementById('hl').innerHTML = html;
|
||||
var items = document.getElementById('hl').querySelectorAll('.hi');
|
||||
for(var j=0;j<items.length;j++){
|
||||
items[j].onclick = (function(u){ return function(){ navigator.clipboard.writeText(u); this.textContent='Скопировано!'; }; })(items[j].getAttribute('data-url'));
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
|
||||
@app.route('/upload/')
|
||||
def index():
|
||||
return PAGE
|
||||
|
||||
|
||||
@app.route('/upload/api', methods=['POST'])
|
||||
def upload_file():
|
||||
if 'file' not in request.files:
|
||||
return jsonify({'error': 'Нет файла'}), 400
|
||||
|
||||
f = request.files['file']
|
||||
if not f.filename:
|
||||
return jsonify({'error': 'Пустое имя файла'}), 400
|
||||
|
||||
ext = f.filename.rsplit('.', 1)[-1].lower() if '.' in f.filename else ''
|
||||
if ext not in ALLOWED:
|
||||
return jsonify({'error': 'Формат .{} не поддерживается'.format(ext)}), 400
|
||||
|
||||
original_name = os.path.splitext(f.filename)[0]
|
||||
|
||||
# HEIC/HEIF → конвертируем в JPG
|
||||
is_heic = ext in ('heic', 'heif')
|
||||
out_ext = 'jpg' if is_heic else ext
|
||||
s3_key = '{}{}.{}'.format(PREFIX, original_name, out_ext)
|
||||
|
||||
tmp_path = None
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix='.{}'.format(ext)) as tmp:
|
||||
f.save(tmp.name)
|
||||
tmp_path = tmp.name
|
||||
|
||||
if is_heic:
|
||||
img = Image.open(tmp_path)
|
||||
converted = tmp_path.replace('.{}'.format(ext), '.jpg')
|
||||
img.convert('RGB').save(converted, 'JPEG', quality=92)
|
||||
os.unlink(tmp_path)
|
||||
tmp_path = converted
|
||||
|
||||
import urllib3
|
||||
urllib3.disable_warnings()
|
||||
|
||||
s3 = boto3.client('s3', verify=False, **S3_CONFIG)
|
||||
s3.upload_file(
|
||||
tmp_path, BUCKET, s3_key,
|
||||
ExtraArgs={
|
||||
'ContentType': MIME.get(ext, 'application/octet-stream'),
|
||||
'CacheControl': 'max-age=31536000'
|
||||
}
|
||||
)
|
||||
|
||||
url = 'https://s3.regru.cloud/{}/{}'.format(BUCKET, s3_key)
|
||||
# hint: HEIC was converted to JPG
|
||||
return jsonify({'url': url})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
finally:
|
||||
if tmp_path and os.path.exists(tmp_path):
|
||||
os.unlink(tmp_path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='127.0.0.1', port=9001, debug=False)
|
||||
Reference in New Issue
Block a user