You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
242 lines
8.6 KiB
242 lines
8.6 KiB
# coding=utf-8
|
|
from PIL import Image
|
|
import os
|
|
from django.core.files.base import File
|
|
from access.models import User
|
|
from lms import settings
|
|
from lms.decors import api_decor
|
|
from lms.tools import translit
|
|
from storage.models import Storage, CroppedImage, FormatIndex
|
|
|
|
IMAGE_TMP = settings.MEDIA_ROOT + 'tmp/'
|
|
|
|
|
|
def save_gif(pil_img, path):
|
|
if 'transparency' in pil_img.info:
|
|
transparency = pil_img.info['transparency']
|
|
pil_img.save(path, 'GIF', transparency=transparency)
|
|
else:
|
|
pil_img.save(path, 'GIF')
|
|
|
|
|
|
def save_resize_image(filename, pil_img, save_field, file_format):
|
|
temp_image = open(os.path.join(IMAGE_TMP, filename), 'wb')
|
|
if file_format.upper() == 'GIF':
|
|
save_gif(pil_img, temp_image)
|
|
else:
|
|
pil_img.save(temp_image, file_format)
|
|
thumb_data = open(os.path.join(IMAGE_TMP, filename), 'rb')
|
|
thumb_file = File(thumb_data)
|
|
save_field.save(str(filename) + '.' + file_format, thumb_file)
|
|
|
|
|
|
def local_get_current_image_clip_id(obj_in, id_in, need_object=False):
|
|
result = {'code': True, 'object': None, 'response': ''}
|
|
|
|
try:
|
|
if obj_in == 'avatar':
|
|
result['response'], result['object'] = User.objects.get(id=id_in).avatar, \
|
|
User.objects.get(id=id_in) if need_object else ''
|
|
|
|
else:
|
|
result['code'] = False
|
|
result['response'] = u'Объект не найден'
|
|
|
|
except:
|
|
result['code'] = False
|
|
result['response'] = u'ID не ссылается на существующий объект %s' % obj_in
|
|
|
|
return result
|
|
|
|
|
|
@api_decor(without_auth=True, need_keys=['key', 'type_in'], check_request=True)
|
|
def check_file_load(request, context):
|
|
context['code'] = '0'
|
|
type_in = request.GET['type_in']
|
|
try:
|
|
original = Storage.objects.get(key=request.GET['key'], loaded=True, error=False)
|
|
except Storage.DoesNotExist:
|
|
context['code'] = '0'
|
|
else:
|
|
if type_in == 'loaded':
|
|
context['code'] = '1'
|
|
elif type_in == 'croped':
|
|
if CroppedImage.objects.filter(original=original, cropped=True).exists():
|
|
context['code'] = '1'
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=True, need_keys=['key'], check_request=True)
|
|
def check_file_error(request, context):
|
|
context['code'] = '0'
|
|
if Storage.objects.filter(key=request.GET['key'], error=True).exists():
|
|
context['code'] = '1'
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=True, need_keys=['key'], check_request=True)
|
|
def get_image_url(request, context):
|
|
context['code'] = '1'
|
|
try:
|
|
file_in = Storage.objects.get(key=request.GET['key'])
|
|
except Storage.DoesNotExist:
|
|
context['code'] = '0'
|
|
context['response'] = u'Такого файла не существует'
|
|
else:
|
|
type_in = ''
|
|
if request.GET.get('type_in'):
|
|
type_in = request.GET['type_in']
|
|
context['data'] = file_in.get_url(type_in)
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=True, need_keys=['obj', 'id'], check_request=True)
|
|
def get_current_image_clip_id(request, context):
|
|
obj_in = request.GET['obj']
|
|
id_in = request.GET['id']
|
|
result = local_get_current_image_clip_id(obj_in, id_in)
|
|
|
|
if result['code']:
|
|
context['code'] = '1'
|
|
context['data'] = result['response']
|
|
else:
|
|
context['code'] = '0'
|
|
context['response'] = result['response']
|
|
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=True, need_keys=['obj', 'id'], check_request=True)
|
|
def del_image(request, context):
|
|
clip_data = local_get_current_image_clip_id(request.GET['obj'], request.GET['id'], need_object=True)
|
|
|
|
if clip_data['code']:
|
|
context['code'] = '1'
|
|
try:
|
|
Storage.objects.get(key=clip_data['response']).delete()
|
|
except Storage.DoesNotExist:
|
|
pass
|
|
clip_data['object'].clean_image()
|
|
|
|
else:
|
|
context['response'] = clip_data['response']
|
|
context['code'] = '0'
|
|
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=True, need_keys=['key'], method='GET', check_request=True)
|
|
def get_file_sketch(request, context):
|
|
try:
|
|
__file = Storage.objects.get(key=request.GET['key'])
|
|
except Storage.DoesNotExist:
|
|
context['code'] = '0'
|
|
else:
|
|
context['code'] = '1'
|
|
context['data'] = {'icon': __file.f_format.icon_class, 'name': __file.get_name_for_user(), 'file_name': __file.get_file_name()}
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=True, need_keys=['clip_file_id'], method='POST', check_request=True)
|
|
def upload_files(request, context):
|
|
min_size = ((256), (256))
|
|
if request.FILES.get('file_load'):
|
|
file_name = request.FILES['file_load'].name
|
|
file_in = Storage.objects.create(key=request.POST['clip_file_id'])
|
|
file_in.original.save(translit(file_name).lower(), File(request.FILES['file_load']))
|
|
try:
|
|
file_in.f_format = FormatIndex.objects.get(name=str(file_in.original.name.split('.')[1]).upper())
|
|
except FormatIndex.DoesNotExist:
|
|
file_in.f_format = FormatIndex.objects.get(name='*')
|
|
else:
|
|
file_in.save()
|
|
file_in.loaded = True
|
|
file_in.save()
|
|
|
|
elif request.FILES.get('file'):
|
|
file_name = request.FILES['file'].name
|
|
file_in = Storage.objects.create(key=request.POST['clip_file_id'])
|
|
file_in.original.save(translit(file_name).lower(), File(request.FILES['file']))
|
|
|
|
try:
|
|
file_in.f_format = FormatIndex.objects.get(name=str(file_in.original.name.split('.')[1]).upper())
|
|
except FormatIndex.DoesNotExist:
|
|
file_in.f_format = FormatIndex.objects.get(name='*')
|
|
else:
|
|
file_in.save()
|
|
|
|
if file_in.f_format.f_type == 'I':
|
|
im = Image.open(file_in.original.path)
|
|
size = im.size
|
|
file_path = file_in.original.path
|
|
if size < min_size:
|
|
context['code'] = '0'
|
|
file_in.error = True
|
|
file_in.save()
|
|
context['response'] = u'Минимальный размер изображения %sx%s' % (min_size[0], min_size[1])
|
|
else:
|
|
im.thumbnail((640, 600), Image.ANTIALIAS)
|
|
if size != im.size:
|
|
if str(file_in.original.name.split('.')[1]).upper() == 'GIF':
|
|
save_gif(im, file_path)
|
|
else:
|
|
im.save(file_path)
|
|
file_in.loaded = True
|
|
file_in.save()
|
|
context['code'] = '1'
|
|
else:
|
|
file_in.loaded = True
|
|
file_in.save()
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=True, check_request=True, need_keys=['x', 'x1', 'y', 'y1', 'w', 'h', 'clip_id'])
|
|
def crop_image(request, context):
|
|
small = (50, 50)
|
|
middle = (100, 100)
|
|
big = (256, 256)
|
|
x = int(request.GET['x'])
|
|
x1 = int(request.GET['x1'])
|
|
y = int(request.GET['y'])
|
|
y1 = int(request.GET['y1'])
|
|
w = int(request.GET['w'])
|
|
h = int(request.GET['h'])
|
|
clip_ip = request.GET['clip_id']
|
|
file_format = ''
|
|
try:
|
|
original = Storage.objects.get(key=clip_ip, loaded=True)
|
|
except Storage.DoesNotExist:
|
|
context['code'] = '0'
|
|
context['response'] = u'Файла не существует'
|
|
else:
|
|
file_format = str(original.original.name.split('.')[1])
|
|
file_format = 'jpeg' if file_format == 'jpg' else file_format
|
|
|
|
im1 = Image.open(original.original.path)
|
|
im = im1.crop((x, x1, y, y1))
|
|
im.info = im1.info
|
|
if file_format.upper() == 'GIF':
|
|
save_gif(im, original.original.path)
|
|
else:
|
|
im.save(original.original.path, file_format)
|
|
|
|
im = Image.open(original.original.path)
|
|
|
|
croped_im = CroppedImage.objects.get(original=original)
|
|
big_img = im.copy()
|
|
big_img.thumbnail(big, Image.ANTIALIAS)
|
|
save_resize_image(clip_ip+'_big', big_img, croped_im.big, file_format)
|
|
|
|
middle_img = im.copy()
|
|
middle_img.thumbnail(middle, Image.ANTIALIAS)
|
|
save_resize_image(clip_ip+'_middle', middle_img, croped_im.middle, file_format)
|
|
|
|
small_img = im.copy()
|
|
small_img.thumbnail(small, Image.ANTIALIAS)
|
|
save_resize_image(clip_ip+'_small', small_img, croped_im.small, file_format)
|
|
|
|
if croped_im.big and croped_im.middle and croped_im.small:
|
|
context['code'] = '1'
|
|
croped_im.cropped = True
|
|
croped_im.save()
|
|
return context
|
|
|