def GetAttachmentList(path, base=0): try: fp = open(path, 'rb') except IOError: raise ServerError('fail to load post') attachlist = [] try: start = base offset = Post.SeekAttachment(fp, start) while (start != offset): # read the name name = Util.ReadString(fp) attach = {'name': Util.gbkDec(name), 'offset': offset} attachlist.append(attach) # read the size s = fp.read(4) size = struct.unpack('!I', s)[0] # big endian start = fp.tell() + size # seek to next attachment offset = Post.SeekAttachment(fp, start) finally: fp.close() return attachlist
def AppendAttachFrom(self, other, attach_entry): with open(other.path, "rb") as fp: fp.seek(attach_entry['offset'] - 8) # check for attachment mark if (fp.read(8) != '\0\0\0\0\0\0\0\0'): raise IOError # read the name name = Util.ReadString(fp) # read the size s = fp.read(4) size = struct.unpack('!I', s)[0] # big endian return Post.AddAttachFrom(self.file, attach_entry['name'], fp, size)
def ReadAttachment(filename, offset): fp = open(filename, "rb") try: fp.seek(offset - 8) # check if is an atachment if (fp.read(8) != '\0\0\0\0\0\0\0\0'): raise IOError # read the name name = Util.ReadString(fp).decode('gbk') # read the size s = fp.read(4) size = struct.unpack('!I', s)[0] # big endian # read the content content = fp.read(size) finally: fp.close() return (name, content)
def LoadFavBoards(self): path = User.User.OwnFile(self._userid, "favboard") self._current = -1 fd = open(path, "rb") if fd != None: magic = Util.ReadInt(fd) if magic != 0x8080: self._count = magic index = 0 while index < self._count: bindex = Util.ReadInt(fd) self._favboards[index] = FavBoard(bindex) index = index + 1 else: self._count = Util.ReadInt(fd) index = 0 while index < self._count: flag = Util.ReadInt(fd) title = '' if flag == -1: length = Util.ReadChar(fd) title = Util.gbkDec(Util.CString(fd.read(length))) father = Util.ReadInt(fd) self._favboards[index] = FavBoard(flag, title, father) index = index + 1 fd.close() if self._count <= 0: fd = open(Config.BBS_ROOT + "etc/initial_favboard", "r") if fd == None: self._count = 1 self._favboards[0] = FavBoard(0) else: self._count = 1 self._favboards[0] = FavBoard(0) while True: board = Util.ReadString(fd) if board == '': break bobj = BoardManager.GetBoard(board) if bobj != None: self._favboards[self._count] = FavBoard(bobj.index - 1) fd.close() else: count = self._count index = 0 while index < self._count: fboard = self._favboards[index] if fboard.IsDir(): index = index + 1 continue bindex = fboard._index board = BoardManager.GetBoardByIndex(bindex + 1) user = UserManager.LoadUser(self._userid) if ((bindex >= 0) and (bindex <= BCache.GetBoardCount()) and (user != None) and (board != None) and (board.CheckSeePerm(user))): index = index + 1 continue self.DelFavBoard(index) index = index + 1 if count != self._count: self.SaveFavBoards()