max folder size

This commit is contained in:
ingvar1995 2016-10-02 20:19:39 +03:00
parent 862d6a170e
commit 3ea8c049c5
5 changed files with 34 additions and 7 deletions

20
bot.py
View File

@ -3,7 +3,7 @@ import os
from settings import *
from toxcore_enums_and_consts import *
from ctypes import *
from util import Singleton
from util import Singleton, folder_size
from file_transfers import *
from collections import defaultdict
@ -112,6 +112,7 @@ class Bot(Singleton):
message <ToxID> <message_text> - send message to friend (masters only)\n
message --all <message_text> - send message to all friends (masters only)\n
stop - stop bot (masters only)\n
fsize <folder_size_in_MB> - set folder size in MB (masters only)\n
Users with write access can send files to bot.
""".encode('utf-8'))
elif message == 'rights': # get rights
@ -216,7 +217,7 @@ class Bot(Singleton):
elif bytes_size < 1024 * 1024:
size = u'{} KB'.format(bytes_size / 1024)
else:
size = u'{} MB'.format(bytes_size / 1024 * 1024)
size = u'{} MB'.format(bytes_size / (1024 * 1024))
s = u'Size: {} ({} bytes)'.format(size, bytes_size)
self.send_message(friend_num, s.encode('utf-8'))
elif message.startswith('message '): # send message to friend (all friends)
@ -264,6 +265,17 @@ class Bot(Singleton):
raise SystemExit()
else:
self.send_message(friend_num, 'Not enough rights'.encode('utf-8'))
elif message.startswith('fsize '):
if id not in settings['master']:
self.send_message(friend_num, 'Not enough rights'.encode('utf-8'))
else:
try:
size = int(message[6:])
settings['size'] = max(size, 10)
settings.save()
self.send_message(friend_num, 'Size was set'.encode('utf-8'))
except:
self.send_message(friend_num, 'Wrong command'.encode('utf-8'))
else:
self.send_message(friend_num, 'Wrong command'.encode('utf-8'))
@ -307,7 +319,9 @@ class Bot(Singleton):
"""
id = self._tox.friend_get_public_key(friend_number)
settings = Settings.get_instance()
if id in settings['write']:
fsize = folder_size(settings['folder']) + size
max_folder_size = settings['size'] * 1024 * 1024
if id in settings['write'] and fsize <= max_folder_size:
path = settings['folder']
new_file_name, i = file_name, 1
while os.path.isfile(path + '/' + new_file_name): # file with same name already exists

View File

@ -1,5 +1,5 @@
from toxcore_enums_and_consts import TOX_FILE_KIND, TOX_FILE_CONTROL
from os.path import basename, getsize
from os.path import basename, getsize, remove
from os import remove
from time import time
from tox import Tox
@ -25,7 +25,7 @@ class FileTransfer(object):
self.state = TOX_FILE_TRANSFER_STATE['RUNNING']
self._file_number = file_number
self._creation_time = time()
self._size = float(size)
self._size = size
self._done = 0
def set_tox(self, tox):
@ -123,3 +123,6 @@ class ReceiveTransfer(FileTransfer):
if position + l > self._file_size:
self._file_size = position + l
self._done += l
if self._done > self._size:
self.cancel()
remove(self._path)

View File

@ -1 +1 @@
{"write": ["56A1ADE4B65B86BCD51CC73E2CD4E542179F47959FE3E0E21B4B0ACDADE51855"], "auto_rights": "r", "master": [], "read": ["56A1ADE4B65B86BCD51CC73E2CD4E542179F47959FE3E0E21B4B0ACDADE51855"], "folder": "/home/tox_user/tox/shared", "delete": ["56A1ADE4B65B86BCD51CC73E2CD4E542179F47959FE3E0E21B4B0ACDADE51855"]}
{"write": ["56A1ADE4B65B86BCD51CC73E2CD4E542179F47959FE3E0E21B4B0ACDADE51855"], "auto_rights": "r", "master": [], "read": ["56A1ADE4B65B86BCD51CC73E2CD4E542179F47959FE3E0E21B4B0ACDADE51855"], "folder": "/home/tox_user/tox/shared", "size": 500, "delete": ["56A1ADE4B65B86BCD51CC73E2CD4E542179F47959FE3E0E21B4B0ACDADE51855"]}

View File

@ -31,7 +31,8 @@ class Settings(Singleton, dict):
'delete': [],
'master': [],
'folder': curr_directory(),
'auto_rights': 'r'
'auto_rights': 'r',
'size': 500
}
def save(self):

View File

@ -10,6 +10,15 @@ def curr_directory():
return os.path.dirname(os.path.realpath(__file__))
def folder_size(path):
size = 0
for f in os.listdir(path):
f = unicode(f)
if os.path.isfile(os.path.join(path, f)):
size += os.path.getsize(os.path.join(path, f))
return size
class Singleton(object):
def __new__(cls, *args, **kwargs):