[sync] image uploading
This commit is contained in:
1 parent
18150e8b35
commit
7c2d4dfd62
2 files changed
+30
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import os
|
||||
from io import BytesIO
|
||||
|
||||
import requests
|
||||
|
||||
from utils.fn import combine, lmap
|
||||
|
||||
|
||||
@@ -83,3 +85,23 @@ def list_images(provider, path):
|
||||
lambda ext: provider.type_filter(path, ext),
|
||||
image_types
|
||||
))
|
||||
|
||||
|
||||
def upload(file_path, relative_path):
|
||||
"""
|
||||
/path/to/file, /url/path -> https://static.shlisselburg.org/art/url/path
|
||||
:param file_path:
|
||||
:param relative_path:
|
||||
:return:
|
||||
"""
|
||||
files = {'file': open(file_path, 'rb')}
|
||||
url = 'https://static.shlisselburg.org/upload/art{}'.format(relative_path)
|
||||
r = requests.put(
|
||||
url=url,
|
||||
files=files,
|
||||
auth=('uploader', '1207690962638465')
|
||||
)
|
||||
if r.status_code == 200:
|
||||
return url
|
||||
else:
|
||||
return None
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from sync.data import upload
|
||||
from utils.hash import hash_file
|
||||
# from utils.image import resize
|
||||
from utils.image.resize import optimize, thumbnail, orient
|
||||
@@ -43,7 +44,12 @@ def create_image(file, sizes, url_fn, output_dir, skip_processing=False):
|
||||
|
||||
if not skip_processing:
|
||||
image = process_image(file, local_image_path, size)
|
||||
if not image:
|
||||
continue
|
||||
width, height = image.size
|
||||
upload_url = upload(local_image_path, '/images/{}'.format(image_filename))
|
||||
if not upload_url:
|
||||
print('Image {} not uploaded'.format(file))
|
||||
|
||||
result[size_name] = {
|
||||
'url': image_url,
|
||||
@@ -66,6 +72,8 @@ def process_image(input_file, output_file, size):
|
||||
return optimize(input_file, output_file, quality=90)
|
||||
else:
|
||||
image = read_image(input_file)
|
||||
if not image:
|
||||
return None
|
||||
image_width, image_height = image.size
|
||||
if image_height > image_width:
|
||||
width, height = height, width
|
||||
|
||||
Reference in new issue
Block a user