|
|
|
@ -29,6 +29,14 @@ class FILETYPE(enum.Enum):
|
|
|
|
|
png = 0
|
|
|
|
|
jpeg = 1
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def file_ext(self):
|
|
|
|
|
return 'png' if self is FILETYPE.png else 'jpg'
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_jpeg(self):
|
|
|
|
|
return self == FILETYPE.jpeg
|
|
|
|
|
|
|
|
|
|
class RATING(enum.Enum):
|
|
|
|
|
safe = 0
|
|
|
|
|
questionable = 1
|
|
|
|
@ -182,19 +190,19 @@ class Post(TimestampMixin, db.Model):
|
|
|
|
|
#tags = db.relationship('Tag', secondary=post_tags, back_populates='posts')
|
|
|
|
|
tags = db.relationship('Tag', secondary=post_tags, backref=db.backref('posts'))
|
|
|
|
|
|
|
|
|
|
def __init__(self, file, **kwargs):
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
# def __init__(self, **kwargs):
|
|
|
|
|
# super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
# self.md5 = hashlib.md5(file.data.getbuffer()).hexdigest()
|
|
|
|
|
# self.filetype = FILETYPE[file.mimetype.split('/')[1]]
|
|
|
|
|
# # self.md5 = hashlib.md5(file.data.getbuffer()).hexdigest()
|
|
|
|
|
# # self.filetype = FILETYPE[file.mimetype.split('/')[1]]
|
|
|
|
|
|
|
|
|
|
# with Image.open(file.data) as im:
|
|
|
|
|
# self.width, self.height = im.width, im.height
|
|
|
|
|
# self.filesize = file.data.getbuffer().nbytes
|
|
|
|
|
# # with Image.open(file.data) as im:
|
|
|
|
|
# # self.width, self.height = im.width, im.height
|
|
|
|
|
# # self.filesize = file.data.getbuffer().nbytes
|
|
|
|
|
|
|
|
|
|
# self.origin_filename = file.filename
|
|
|
|
|
# # self.origin_filename = file.filename
|
|
|
|
|
|
|
|
|
|
self.generate_image_files(file)
|
|
|
|
|
# self.generate_image_files()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def resolution(self):
|
|
|
|
@ -214,25 +222,24 @@ class Post(TimestampMixin, db.Model):
|
|
|
|
|
origin_filename=file.filename
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def generate_image_files(self, file):
|
|
|
|
|
# with open(self.image_files['image'], "wb") as f:
|
|
|
|
|
# f.write(file.data.getbuffer())
|
|
|
|
|
file.seek(0)
|
|
|
|
|
file.save(self.image_files['image'])
|
|
|
|
|
|
|
|
|
|
with Image.open(file) as im:
|
|
|
|
|
def generate_image_files(self):
|
|
|
|
|
with Image.open(self.image_files['image']) as im:
|
|
|
|
|
im = im.convert('RGB')
|
|
|
|
|
|
|
|
|
|
if self.image_files['jpeg'] is not None:
|
|
|
|
|
im.save(self.image_files['jpeg'], 'JPEG', quality=80)
|
|
|
|
|
im.save(self.image_files['jpeg'], 'JPEG', quality=90) # SUM QUALITY, YEEES
|
|
|
|
|
|
|
|
|
|
sim = im.copy()
|
|
|
|
|
sim.thumbnail([800,800])
|
|
|
|
|
sim.thumbnail([1500,1500])
|
|
|
|
|
sim.save(self.image_files['sample'], 'JPEG', quality=80)
|
|
|
|
|
|
|
|
|
|
sim = im.copy()
|
|
|
|
|
sim.thumbnail([800,800])
|
|
|
|
|
sim.save(self.image_files['thumb_big'], 'JPEG', quality=80)
|
|
|
|
|
|
|
|
|
|
sim = im.copy()
|
|
|
|
|
sim.thumbnail([512,512])
|
|
|
|
|
sim.save(self.image_files['thumb'], 'JPEG', quality=80)
|
|
|
|
|
sim.save(self.image_files['thumb_smol'], 'JPEG', quality=80)
|
|
|
|
|
|
|
|
|
|
def remove_image_files(self):
|
|
|
|
|
for im in self.image_files.values():
|
|
|
|
@ -244,30 +251,45 @@ class Post(TimestampMixin, db.Model):
|
|
|
|
|
@property
|
|
|
|
|
def image_files(self):
|
|
|
|
|
return dict(
|
|
|
|
|
image=os.path.join(current_app.config.get('POST_UPLOADS'), 'img', "{}.{}".format(self.md5, 'jpg' if self.filetype is FILETYPE.jpeg else 'png')),
|
|
|
|
|
jpeg=(None if self.filetype is FILETYPE.jpeg else os.path.join(current_app.config.get('POST_UPLOADS'), 'jpeg', "{}.{}".format(self.md5, 'jpg'))),
|
|
|
|
|
# image=os.path.join(current_app.config.get('POST_UPLOADS'), 'img', "{}.{}".format(self.md5, 'jpg' if self.filetype is FILETYPE.jpeg else 'png')),
|
|
|
|
|
image=os.path.join(current_app.config.get('POST_UPLOADS'), 'img', "{}.{}".format(self.md5, self.filetype.file_ext)),
|
|
|
|
|
jpeg=(None if self.filetype.is_jpeg else os.path.join(current_app.config.get('POST_UPLOADS'), 'jpeg', "{}.{}".format(self.md5, 'jpg'))),
|
|
|
|
|
sample=os.path.join(current_app.config.get('POST_UPLOADS'), 'sample', "{}.{}".format(self.md5, 'jpg')),
|
|
|
|
|
thumb=os.path.join(current_app.config.get('POST_UPLOADS'), 'thumb', "{}.{}".format(self.md5, 'jpg'))
|
|
|
|
|
thumb_big=os.path.join(current_app.config.get('POST_UPLOADS'), 'thumb_big', "{}.{}".format(self.md5, 'jpg')),
|
|
|
|
|
thumb_smol=os.path.join(current_app.config.get('POST_UPLOADS'), 'thumb', "{}.{}".format(self.md5, 'jpg'))
|
|
|
|
|
)
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return('<Post #{} by {}, {} of {} bytes>'.format(self.id, self.author, self.filetype.name, self.filesize))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def file_uri(self):
|
|
|
|
|
# filename = "{}.{}".format('maybe_later_generated_cute_filename', 'jpg' if self.filetype is FILETYPE.jpeg else 'png')
|
|
|
|
|
# filename = 'maybe_later_generated_cute_filename'
|
|
|
|
|
filename = "{} - {} {}".format(current_app.config.get('INSTANCE_NAME'), self.id, " ".join(tag.content.replace(' ', '_') for tag in self.tags))
|
|
|
|
|
return os.path.join(self.md5, filename)
|
|
|
|
|
|
|
|
|
|
def url(self, path, endpoint='img'):
|
|
|
|
|
if endpoint == 'img':
|
|
|
|
|
return url_for('main.uploaded_img', path="{}.{}".format(path,'jpg' if self.filetype is FILETYPE.jpeg else 'png'))
|
|
|
|
|
return url_for(
|
|
|
|
|
'main.uploaded_img',
|
|
|
|
|
path="{}.{}".format(path, self.filetype.file_ext)
|
|
|
|
|
)
|
|
|
|
|
elif endpoint == 'jpeg':
|
|
|
|
|
return url_for('main.uploaded_jpeg' if not self.filetype is FILETYPE.jpeg else 'main.uploaded_img', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
return url_for(
|
|
|
|
|
'main.uploaded_jpeg' if not self.filetype.is_jpeg else 'main.uploaded_img',
|
|
|
|
|
path="{}.{}".format(path,'jpg')
|
|
|
|
|
)
|
|
|
|
|
elif endpoint == 'sample':
|
|
|
|
|
return url_for('main.uploaded_sample', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
elif endpoint == 'thumb_big':
|
|
|
|
|
return url_for('main.uploaded_big_thumb', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
elif endpoint == 'thumb':
|
|
|
|
|
return url_for('main.uploaded_thumb', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
return url_for('main.uploaded_smol_thumb', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def file_uri(self):
|
|
|
|
|
return os.path.join(
|
|
|
|
|
self.md5,
|
|
|
|
|
"{} - {} {}".format(
|
|
|
|
|
current_app.config.get('INSTANCE_NAME'),
|
|
|
|
|
self.id,
|
|
|
|
|
" ".join(tag.content.replace(' ', '_') for tag in self.tags)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return('<Post #{} by {}, {} of {} bytes>'.format(self.id, self.author, self.filetype.name, self.filesize))
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def flex(self):
|
|
|
|
|