Exemplo n.º 1
0
 def utimens(self, path, times=None):
     if times is not None:
         access_time, modified_time = times
         file_id = self._get_path_ids(path)[-1]
         File.update(
             updated_at=datetime.datetime.fromtimestamp(modified_time),
             accessed_at=datetime.datetime.fromtimestamp(
                 access_time)).where(File.id == file_id).execute()
Exemplo n.º 2
0
    def rename(self, old, new):  # Also mv
        dir_ids_org = self._get_path_ids(old)

        dirs_new = new.split('/')
        subpath_new = '/'.join(dirs_new)[:-1]
        dir_ids_new = self._get_path_ids(subpath_new)
        name_new = dirs_new[-1]

        f_org = File.get(id=dir_ids_org[-1])
        File.update(
            name=name_new,
            parent_file=dir_ids_new[-1]).where(File.id == f_org.id).execute()
Exemplo n.º 3
0
 def mkdir(self, path, mode):
     dirs = path.split('/')
     subpath = '/'.join(dirs[:-1])
     new_dir = dirs[-1]
     file_id = self._get_path_ids(subpath)[-1]
     f = File.get(id=file_id)
     File(name=new_dir,
          telegram_file_id=None,
          parent_file=f.id,
          size=512,
          user_owner=self._uid,
          group_owner=self._gid,
          is_directory=True,
          mode=16877).save()
Exemplo n.º 4
0
 def readlink(self, path):
     file_id = self._get_path_ids(path)[-1]
     f = File.get(id=file_id)
     if f.sym_link.startswith("/"):
         return path + f.sym_link
     else:
         return f.sym_link
Exemplo n.º 5
0
 def rmdir(self, path):
     file_id = self._get_path_ids(path)[-1]
     f = File.get(id=file_id)
     if not f.is_directory:
         raise FuseOSError(errno.ENOTDIR)
     elif f.files.count() > 0:
         raise FuseOSError(errno.ENOTEMPTY)
     else:
         f.delete().where(File.id == file_id).execute()
Exemplo n.º 6
0
    def readdir(self, path, fh):
        file_id = self._get_path_ids(path)[-1]
        f = File.get(id=file_id)

        dirents = ['.', '..']
        if f.is_directory:
            for each in f.files:
                dirents.append(each.name)
        for r in dirents:
            yield r
Exemplo n.º 7
0
    def __init__(self, tg_token, chat_id):
        self._uid = os.getuid()
        self._gid = os.getgid()

        File.get_or_create(id=1,
                           name='$',
                           telegram_file_id=None,
                           parent_file_id=None,
                           size=512,
                           user_owner=self._uid,
                           group_owner=self._gid,
                           sym_link=None,
                           is_directory=1,
                           mode=16895)
        self.tg_token = tg_token
        self.tgbot = telebot.TeleBot(tg_token)
        self.chat_id = chat_id
        self._tgchunk_size = 20 * 1024 * 1024
        self.tempfiles = {}
        Thread(target=self.upload_files_daemon).start()
Exemplo n.º 8
0
 def getattr(self, path, fh=None):
     file_id = self._get_path_ids(path)[-1]
     f = File.get(id=file_id)
     return {
         'st_atime': int(f.accessed_at.timestamp()),
         'st_uid': self._uid,
         'st_gid': self._uid,
         'st_mode': f.mode,
         'st_mtime': int(f.updated_at.timestamp()),
         'st_nlink': 0,
         'st_size': f.size
     }
Exemplo n.º 9
0
 def create(self, path, mode, fi=None):
     dirs = path.split('/')
     subpath = '/'.join(dirs[:-1])
     filename = dirs[-1]
     file_id = self._get_path_ids(subpath)[-1]
     return File(
         name=filename,
         parent_file=file_id,
         size=0,
         mode=33188,
         is_directory=False,
     ).save()
Exemplo n.º 10
0
    def _get_path_ids(self, path):
        names = list(filter(None, path.split('/')))
        parent_id = 1
        ids = [1]
        for name in names:
            f = File.get_or_none(File.name == name,
                                 File.parent_file == parent_id)
            if f is None:
                raise FuseOSError(errno.ENOENT)
            ids.append(f.id)
            parent_id = f.id

        return ids
Exemplo n.º 11
0
	def post(self):		
		file1 = self.request.files['file'][0]
		original_fname = file1['filename']
		user=self.get_argument("user")#nombre de usuario
		user_id=self.get_argument("user_id")#id de usuario
		die=self.get_argument("datepicker")#fecha de expiracion
		output_file = open("uploads/"+user+"/" + original_fname, 'wb')
		output_file.write(file1['body'])
		#bbdd
		_file=File(name=original_fname,user_id=int(user_id),expiration=die)
		s=session()
		s.add(_file)
		s.commit()
		s.close()
		self.finish("file " + original_fname + " is uploaded")	
Exemplo n.º 12
0
 def json_files_unpack(self, json_str, target):
     """
     Generate a list of File objects from a JSON directory tree
     json_str: json object as string; raise error when None
     target: a list to put file objects in
     """
     if json_str is None:
         self.json_error(400, "Please supply files")
     try:
         json_obj = json.loads(json_str)
     except json.decoder.JSONDecodeError:
         self.json_error(400, "Files cannot be JSON decoded")
     content_list = []
     for i in json_obj:
         if not self.path_check(i["path"]):
             self.json_error(400, "Illegal path")
         try:
             content = base64.decodebytes(i["content"].encode())
         except binascii.Error:
             self.json_error(400, "Content cannot be base64 decoded")
         target.append(File(i["path"], content))
         content_list.append(content)
     # Commit files
     storage_path = self.application.storage_path
     os.makedirs(storage_path, exist_ok=True)
     for file_obj, content in zip(target, content_list):
         f = None
         for i in range(10):
             actual_name = self.filename_create(file_obj.filename)
             try:
                 f = open(os.path.join(storage_path, actual_name), "xb")
                 break
             except FileExistsError:
                 pass
         if f is None:
             msg = "Internal server error"
             if self.application.debug:
                 msg += " (filename conflict)"
             raise self.json_error(500, msg)
         f.write(content)
         f.close()
         file_obj.actual_name = actual_name
Exemplo n.º 13
0
    def truncate(self, path, length, fh=None):
        file_id = self._get_path_ids(path)[-1]
        f = File.get(id=file_id)
        telegram_files = [t for t in f.telegram_files]
        nof_chunks = length / self._tgchunk_size
        if len(telegram_files) < nof_chunks:
            return

        TelegramDocument.delete().where(
            TelegramDocument.file_id == f.id,
            TelegramDocument.file_no > nof_chunks).execute()
        last_file = TelegramDocument.get(file_id=f.id, file_no=nof_chunks)
        file_data = self._get_file(last_file.telegram_id)
        buf = tempfile.SpooledTemporaryFile(max_size=1024**3)
        for data in file_data:
            buf.write(data)
        new_telegram_id = self._upload_file(buf)
        last_file.telegram_id = new_telegram_id
        last_file.save()
        f.size = length
        f.save()
Exemplo n.º 14
0
    def read(self, path, length, offset, fh):
        file_id = self._get_path_ids(path)[-1]
        f = File.get(id=file_id)
        telegram_ids = {}

        for telegram_file in f.telegram_files:
            telegram_ids[telegram_file.telegram_id] = telegram_file.file_no

        telegram_ids = sorted(telegram_ids, key=telegram_ids.get)

        telegram_files = []
        for telegram_id in telegram_ids:
            telegram_files.append(self._get_file(telegram_id))

        temp = tempfile.SpooledTemporaryFile(max_size=1024**3)
        for telegram_file in telegram_files:
            file_utils.join(telegram_files, temp)

        temp.seek(offset)
        f.accessed_at = datetime.datetime.now()
        f.save()
        return temp.read(length)
Exemplo n.º 15
0
    def test_insert_files(self):

        db = Database("test.sqlite3")
        website_id = db.insert_website(Website("", "", ""))
        db.insert_files(
            [File(website_id, "/some/dir/", "text/plain", "file.txt", 1234)])

        conn = sqlite3.connect("test.sqlite3")
        cursor = conn.cursor()

        cursor.execute("SELECT * FROM File WHERE id=?", (1, ))
        db_file = cursor.fetchone()

        cursor.execute("SELECT * FROM WebsitePath WHERE id=?", (db_file[1], ))
        db_path = cursor.fetchone()

        self.assertEqual(db_file[0], 1)
        self.assertEqual(db_file[1], db_path[0])
        self.assertEqual(db_file[3], "file.txt")
        self.assertEqual(db_file[4], 1234)
        self.assertEqual(db_path[1], website_id)
        self.assertEqual(db_path[2], "/some/dir/")
Exemplo n.º 16
0
 def unlink(self, path):
     file_id = self._get_path_ids(path)[-1]
     File.delete().where(File.id == file_id).execute()
Exemplo n.º 17
0
    def write(self, path, buf, offset, fh):
        print('Write:')
        print('  offset: ' + str(offset))
        print('  bytes: ' + str(len(buf)))
        if path not in self.tempfiles:
            self.tempfiles[path] = {
                'file': tempfile.SpooledTemporaryFile(max_size=50 * (1024**2))
            }
        self.tempfiles[path]['lastwrite'] = datetime.datetime.now()
        self.tempfiles[path]['file'].seek(offset)
        print(self.tempfiles[path]['file'].write(buf))
        return

        print('Write:')
        print('  offset: ' + str(offset))
        print('  bytes: ' + str(len(buf)))
        file_id = self._get_path_ids(path)[-1]
        f = File.get(id=file_id)

        nof_chunks = int(len(buf) / self._tgchunk_size) + (
            1 if len(buf) != self._tgchunk_size else 0)
        first_chunk_start_position = offset - (offset % self._tgchunk_size)
        first_chunk_no = int(first_chunk_start_position / self._tgchunk_size)

        chunks_streams = []
        for x in range(first_chunk_no, first_chunk_no + nof_chunks):
            tgd, created = TelegramDocument.get_or_create(file_id=f.id,
                                                          file_no=x)
            if created:
                chunks_streams.append(None)
            else:
                chunks_streams.append(self._get_file(tgd.telegram_id))

        for x in range(first_chunk_no, first_chunk_no + nof_chunks):
            chunk_stream = chunks_streams.pop(0)
            chunk_buf = tempfile.SpooledTemporaryFile(max_size=1024**3)
            chunk_buf.seek(0)
            if chunk_stream is not None:
                chunk_buf.write(chunk_stream.read())

            if x is 0:
                start_chunk = offset % self._tgchunk_size
                last_chunk = self._tgchunk_size
                start_buf = start_chunk + (first_chunk_no * self._tgchunk_size)
                last_buf = (first_chunk_no + 1) * self._tgchunk_size
            elif x is (first_chunk_no + nof_chunks - 1):
                start_chunk = 0
                last_chunk = self._tgchunk_size
                start_buf = x * self._tgchunk_size
                last_buf = ((x + 1) * self._tgchunk_size) - 1
            else:
                start_chunk = 0
                last_chunk = (offset + len(buf)) % self._tgchunk_size
                start_buf = x * self._tgchunk_size
                last_buf = start_buf + last_chunk

            chunk_buf.seek(start_chunk)
            print('  bytes to write (' + str(start_buf) + ':' + str(last_buf) +
                  '): ' + str(len(buf[start_buf:last_buf])))
            bytes_written = chunk_buf.write(buf[start_buf:last_buf])
            print('  bytes written: ' + str(bytes_written))
            # chunk_buf[start_chunk:last_chunk] = buf[start_buf:last_buf]
            tg_doc_id = self._upload_file(chunk_buf)
            tgd.telegram_id = tg_doc_id
            tgd.save()

        last_tgd = TelegramDocument.select(
            fn.MAX(TelegramDocument.file_no), TelegramDocument.id,
            TelegramDocument.telegram_id, TelegramDocument.file_no).where(
                TelegramDocument.file_id == f.id).first()
        file_size = last_tgd.file_no * self._tgchunk_size
        file_size += int(
            self._get_file(last_tgd.telegram_id).headers['Content-length'])
        print('Last file size: ' +
              self._get_file(last_tgd.telegram_id).headers['Content-length'])

        f.updated_at = datetime.datetime.now()
        f.size = file_size
        f.save()
        print('\n')
        return f.size
Exemplo n.º 18
0
 def chmod(self, path, mode):
     file_id = self._get_path_ids(path)[-1]
     File.update(mode=mode).where(File.id == file_id).execute()
Exemplo n.º 19
0
 def chown(self, path, uid, gid):
     file_id = self._get_path_ids(path)[-1]
     File.update(user_owner=uid,
                 group_owner=gid).where(File.id == file_id).execute()
Exemplo n.º 20
0
 def symlink(self, src, dst):
     file_id = self._get_path_ids(src)[-1]
     File.update(sym_link=dst).where(File.id == file_id).execute()