2019-12-20 08:43:18 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-08-12 03:32:58 +00:00
|
|
|
|
2019-12-20 08:43:18 +00:00
|
|
|
Class to remember blocks that need to be uploaded
|
|
|
|
and not shared on startup/shutdown
|
2019-09-16 07:50:13 +00:00
|
|
|
"""
|
2019-12-20 08:43:18 +00:00
|
|
|
import atexit
|
2020-01-04 12:13:10 +00:00
|
|
|
import os
|
2019-12-20 08:43:18 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
import deadsimplekv
|
|
|
|
|
|
|
|
import filepaths
|
|
|
|
from onionrutils import localcommand
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from communicator import OnionrCommunicatorDaemon
|
2020-07-27 00:02:39 +00:00
|
|
|
from deadsimplekv import DeadSimpleKV
|
2019-09-16 07:50:13 +00:00
|
|
|
"""
|
2019-08-12 03:32:58 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-09-16 07:50:13 +00:00
|
|
|
"""
|
2019-08-12 03:32:58 +00:00
|
|
|
|
|
|
|
UPLOAD_MEMORY_FILE = filepaths.upload_list
|
|
|
|
|
2019-12-20 08:43:18 +00:00
|
|
|
|
2019-08-12 03:32:58 +00:00
|
|
|
def _add_to_hidden_blocks(cache):
|
|
|
|
for bl in cache:
|
|
|
|
localcommand.local_command('waitforshare/' + bl, post=True)
|
|
|
|
|
2019-12-20 08:43:18 +00:00
|
|
|
|
2019-08-12 03:32:58 +00:00
|
|
|
class UploadQueue:
|
2019-12-20 08:43:18 +00:00
|
|
|
"""Saves and loads block upload info from json file."""
|
2019-08-12 03:32:58 +00:00
|
|
|
|
|
|
|
def __init__(self, communicator: 'OnionrCommunicatorDaemon'):
|
2019-12-20 08:43:18 +00:00
|
|
|
"""Start the UploadQueue object, loading left over uploads into queue.
|
|
|
|
|
|
|
|
register save shutdown function
|
2019-09-16 07:50:13 +00:00
|
|
|
"""
|
2019-08-12 03:32:58 +00:00
|
|
|
self.communicator = communicator
|
2019-12-20 08:43:18 +00:00
|
|
|
cache: deadsimplekv.DeadSimpleKV = deadsimplekv.DeadSimpleKV(
|
|
|
|
UPLOAD_MEMORY_FILE)
|
2020-08-10 06:03:32 +00:00
|
|
|
self.kv: "DeadSimpleKV" = \
|
|
|
|
communicator.shared_state.get_by_string("DeadSimpleKV")
|
2019-08-12 03:32:58 +00:00
|
|
|
self.store_obj = cache
|
2019-12-20 08:43:18 +00:00
|
|
|
cache = cache.get('uploads')
|
|
|
|
if cache is None:
|
2019-08-12 03:32:58 +00:00
|
|
|
cache = []
|
2019-12-20 08:43:18 +00:00
|
|
|
|
2019-08-12 03:32:58 +00:00
|
|
|
_add_to_hidden_blocks(cache)
|
2020-07-27 00:02:39 +00:00
|
|
|
self.kv.get('blocksToUpload').extend(cache)
|
2019-08-12 03:32:58 +00:00
|
|
|
|
|
|
|
atexit.register(self.save)
|
|
|
|
|
|
|
|
def save(self):
|
2019-12-20 08:43:18 +00:00
|
|
|
"""Save to disk on shutdown or if called manually."""
|
2020-07-27 00:02:39 +00:00
|
|
|
bl: deadsimplekv.DeadSimpleKV = self.kv.get('blocksToUpload')
|
2020-01-03 10:14:04 +00:00
|
|
|
if len(bl) == 0:
|
|
|
|
try:
|
|
|
|
os.remove(UPLOAD_MEMORY_FILE)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.store_obj.put('uploads', bl)
|
|
|
|
self.store_obj.flush()
|