Пример #1
0
 def get_media(self, media_id: Union[Media, int]) -> Media:
     """
     :param media_id: when you pass media_id as Media, we do query from the database again
     :return: Media
     """
     if isinstance(media_id, Media):
         media_id = media_id.id
     cur = self.db.cursor()
     cur.execute(
         """
         SELECT * FROM media WHERE id = ?;
         """,
         (media_id,)
     )
     media = cur.fetchall()[0]
     cur.close()
     return Media.from_dict(
         {
             "id": media_id,
             "hash": media[1],
             "filename": media[2],
             "filesize": media[3],
             "caption": media[4],
             "time_add": media[5],
             "type": MediaType(media[6]),
             "sub_type": media[7],
             "type_addition": media[8],
             "series_uuid": media[9],
             "series_no": media[10],
             "comment": media[11]
         },
         self
     )
Пример #2
0
 def add_media(self, path: str, kind: MediaType, sub_kind: str = None, kind_addition: str = None, caption=None,
               comment: str = None) -> Media:
     """
     :param path: path to media indicated how to access media file
     :param kind: media type (use kind to avoid built-in name)
     :param sub_kind: media sub type
     :param kind_addition: type additional message
     :param caption: title of this media
     :param comment: media comment
     :return: integer for media id used for index media
     """
     ori_kind = kind
     kind = kind.value
     if not os.path.isfile(path):
         raise Exception("Not Exists or Not a File")
     with open(path, "rb") as f:
         file_hash = getattr(hashlib, config.HASH_ALGO.lower())(f.read()).hexdigest().upper()
     filename = os.path.basename(path)
     ext = os.path.splitext(path)[-1]
     new_path = file_hash[:2] + '/' + file_hash[2:] + ext
     new_path = self.path + '/' + config.MEDIAS_FOLDER + '/' + new_path
     if os.path.exists(new_path):
         raise Exception("Already Exists")
     os.makedirs(self.path + '/' + config.MEDIAS_FOLDER + '/' + file_hash[:2], exist_ok=True)
     shutil.copy(path, new_path)
     cur = self.db.cursor()
     filesize = os.path.getsize(new_path)
     cur.execute(
         """
         INSERT INTO media (hash, filename, filesize, caption, type, sub_type, type_addition, comment)
         VALUES (?,?,?,?,?,?,?,?);
         """,
         (file_hash, filename, filesize, caption, kind, sub_kind, kind_addition, comment)
     )
     id = cur.lastrowid
     cur.execute(
         """
         SELECT time_add FROM media WHERE id = ?;
         """,
         (id,)
     )
     time_add = cur.fetchall()[0][0]
     cur.close()
     self.db.commit()
     return Media.from_dict(
         {
             "id": id,
             "hash": file_hash,
             "filename": filename,
             "filesize": filesize,
             "caption": caption,
             "time_add": time_add,
             "type": ori_kind,
             "sub_type": sub_kind,
             "type_addition": kind_addition,
             "series_uuid": None,
             "series_no": None,
             "comment": comment
         },
         self
     )