|
|
|
@ -185,49 +185,42 @@ class Post(TimestampMixin, db.Model):
|
|
|
|
|
def __init__(self, file, **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)
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return('<Post #{} by {}, {} of {} bytes>'.format(self.id, self.author, self.filetype.name, self.filesize))
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def flex(self):
|
|
|
|
|
return "flex: {0:.2f} 1 {0:.2f}px;".format(self.width/self.height*current_app.config.get('POST_LIST_THUMB_HEIGHT', 240))
|
|
|
|
|
|
|
|
|
|
@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 resolution(self):
|
|
|
|
|
return (self.width, self.height)
|
|
|
|
|
|
|
|
|
|
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'))
|
|
|
|
|
elif endpoint == 'jpeg':
|
|
|
|
|
return url_for('main.uploaded_jpeg' if not self.filetype is FILETYPE.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':
|
|
|
|
|
return url_for('main.uploaded_thumb', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
@resolution.setter
|
|
|
|
|
def resolution(self, value):
|
|
|
|
|
self.width, self.height = value
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def fileinfo(file):
|
|
|
|
|
return dict(
|
|
|
|
|
md5=(file.seek(0) or hashlib.md5(file.read()).hexdigest()),
|
|
|
|
|
filetype=FILETYPE[file.mimetype.split('/')[1]], # not safe
|
|
|
|
|
resolution=Image.open(file).size,
|
|
|
|
|
filesize=(file.seek(0,2) or file.tell()),
|
|
|
|
|
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(post.image_path)
|
|
|
|
|
# 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.data) as im:
|
|
|
|
|
with Image.open(file) as im:
|
|
|
|
|
im = im.convert('RGB')
|
|
|
|
|
|
|
|
|
|
if self.image_files['jpeg'] is not None:
|
|
|
|
@ -256,6 +249,29 @@ class Post(TimestampMixin, db.Model):
|
|
|
|
|
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'))
|
|
|
|
|
)
|
|
|
|
|
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'))
|
|
|
|
|
elif endpoint == 'jpeg':
|
|
|
|
|
return url_for('main.uploaded_jpeg' if not self.filetype is FILETYPE.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':
|
|
|
|
|
return url_for('main.uploaded_thumb', path="{}.{}".format(path,'jpg'))
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def flex(self):
|
|
|
|
|
return "flex: {0:.2f} 1 {0:.2f}px;".format(self.width/self.height*current_app.config.get('POST_LIST_THUMB_HEIGHT', 240))
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def image_resolution(self):
|
|
|
|
|