mirror of
https://github.com/Mistake35/Cedar-Django.git
synced 2026-07-18 00:21:14 +10:00
File sharing update (requires migrations for existing databases)
There are a bunch of migration files that have been thrown into the repository. Sadly, migrating is a huge pain in the fucking ass, you may need to rollback your database or god knows what. - did not add change logs too lazy
This commit is contained in:
@@ -78,140 +78,6 @@ def recaptcha_verify(request, key):
|
||||
return False
|
||||
return True
|
||||
|
||||
def video_upload(video):
|
||||
hash = sha1()
|
||||
for chunk in video.chunks():
|
||||
hash.update(chunk)
|
||||
imhash = hash.hexdigest()
|
||||
# only either webm or mp4
|
||||
extension = video.name[-4:]
|
||||
if extension == '.mp4':
|
||||
fname = imhash + '.mp4'
|
||||
elif extension == 'webm':
|
||||
fname = imhash + '.webm'
|
||||
else:
|
||||
return 1
|
||||
# simply only write image if the hashed path doesn't already exist
|
||||
if not os.path.exists(settings.MEDIA_ROOT + fname):
|
||||
with open(settings.MEDIA_ROOT + fname, "wb+") as destination:
|
||||
for chunk in video.chunks():
|
||||
destination.write(chunk)
|
||||
return settings.MEDIA_URL + fname
|
||||
|
||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||
def image_upload(img, stream=False, drawing=False, avatar=False, icon=False, banner=False):
|
||||
Imagefield = False
|
||||
if banner or icon:
|
||||
Imagefield = True
|
||||
if stream:
|
||||
decodedimg = img.read()
|
||||
else:
|
||||
try:
|
||||
decodedimg = base64.b64decode(img)
|
||||
except ValueError:
|
||||
return 1
|
||||
if stream:
|
||||
if not 'image' in img.content_type:
|
||||
return 1
|
||||
# upload svg?
|
||||
#if 'svg' in mime:
|
||||
#
|
||||
try:
|
||||
im = Image.open(io.BytesIO(decodedimg))
|
||||
# OSError is probably from invalid images, SyntaxError probably from unsupported images
|
||||
except (OSError, SyntaxError):
|
||||
return 1
|
||||
# Taken from https://coderwall.com/p/nax6gg/fix-jpeg-s-unexpectedly-rotating-when-saved-with-pil
|
||||
if hasattr(im, '_getexif'):
|
||||
orientation = 0x0112
|
||||
exif = im._getexif()
|
||||
if exif is not None:
|
||||
orientation = exif.get(orientation)
|
||||
rotations = {
|
||||
3: Image.ROTATE_180,
|
||||
6: Image.ROTATE_270,
|
||||
8: Image.ROTATE_90
|
||||
}
|
||||
if orientation in rotations:
|
||||
im = im.transpose(rotations[orientation])
|
||||
if avatar or icon:
|
||||
tiny = True
|
||||
# crop 1:1
|
||||
width, height = im.size
|
||||
min_dimension = min(width, height)
|
||||
crop_size = min_dimension
|
||||
left = (width - crop_size) // 2
|
||||
top = (height - crop_size) // 2
|
||||
right = left + crop_size
|
||||
bottom = top + crop_size
|
||||
im = im.crop((left, top, right, bottom))
|
||||
im.thumbnail((256, 256))
|
||||
else:
|
||||
tiny = False
|
||||
im.thumbnail((1280, 1280))
|
||||
|
||||
# Let's check the aspect ratio and see if it's crazy
|
||||
# IF this is a drawing
|
||||
if drawing and ((im.size[0] / im.size[1]) < 0.30):
|
||||
return 1
|
||||
|
||||
# I know some people have aneurysms when they see people actually using SHA1 in the real world, for anything in general.
|
||||
# Yes, we are really using it. Sorry if that offends you. It's just fast and I don't feel I need anything more random, since we are talking about IMAGES.
|
||||
if stream:
|
||||
hash = sha1()
|
||||
for chunk in img.chunks():
|
||||
hash.update(chunk)
|
||||
imhash = hash.hexdigest()
|
||||
else:
|
||||
imhash = sha1(im.tobytes()).hexdigest()
|
||||
# File saving target
|
||||
target = 'png'
|
||||
if stream:
|
||||
# If we have a stream and either a JPEG or a WEBP, save them as those since those are a bit better than plain PNG
|
||||
if 'jpeg' in img.content_type:
|
||||
target = 'jpeg'
|
||||
im = im.convert('RGB')
|
||||
elif 'webp' in img.content_type:
|
||||
target = 'webp'
|
||||
# Janky goofy ahh solution to a stupid problem!
|
||||
if tiny:
|
||||
floc = imhash + '_tiny' + '.' + target
|
||||
else:
|
||||
floc = imhash + '.' + target
|
||||
# If the file exists, just use it, that's what hashes are for.
|
||||
|
||||
if not os.path.exists(settings.MEDIA_ROOT + floc):
|
||||
im.save(settings.MEDIA_ROOT + floc, target, optimize=True)
|
||||
if not Imagefield:
|
||||
return settings.MEDIA_URL + floc
|
||||
# Not compatible with imagefield otherwise, if this is not here, Images uploaded will not show due to an incorrect URL.
|
||||
# yes it's a crack den solution but it works.
|
||||
else: return floc
|
||||
|
||||
# Todo: Put this into post/comment delete thingy method
|
||||
def image_rm(image_url):
|
||||
if settings.IMAGE_DELETE_SETTING:
|
||||
if settings.MEDIA_URL in image_url:
|
||||
sysfile = image_url.split(settings.MEDIA_URL)[1]
|
||||
sysloc = settings.MEDIA_ROOT + sysfile
|
||||
if settings.IMAGE_DELETE_SETTING > 1:
|
||||
try:
|
||||
remove(sysloc)
|
||||
except:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
# The RM'd directory to move it to
|
||||
rmloc = sysloc.replace(settings.MEDIA_ROOT, settings.MEDIA_ROOT + 'rm/')
|
||||
try:
|
||||
rename(sysloc, rmloc)
|
||||
except:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def get_gravatar(email):
|
||||
try:
|
||||
page = urllib.request.urlopen('https://gravatar.com/avatar/'+ md5(email.encode('utf-8').lower()).hexdigest() +'?d=404&s=128')
|
||||
|
||||
Reference in New Issue
Block a user