2019-12-19 10:34:19 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-06-12 20:12:56 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
This file handles the commands for adding
|
|
|
|
and getting files from the Onionr network
|
2019-09-21 23:49:24 +00:00
|
|
|
"""
|
2019-12-19 10:34:19 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import logger
|
|
|
|
from onionrblocks.onionrblockapi import Block
|
|
|
|
import onionrexceptions
|
|
|
|
from onionrutils import stringvalidators
|
|
|
|
from etc import onionrvalues
|
|
|
|
from onionrblocks import insert
|
2019-09-21 23:49:24 +00:00
|
|
|
"""
|
2019-06-12 20:12:56 +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-21 23:49:24 +00:00
|
|
|
"""
|
2019-06-12 20:12:56 +00:00
|
|
|
|
2019-09-10 06:05:59 +00:00
|
|
|
_ORIG_DIR = onionrvalues.ORIG_RUN_DIR_ENV_VAR
|
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
|
|
|
|
def _get_dir(path: str) -> str:
|
|
|
|
if not os.getenv(_ORIG_DIR) is None:
|
|
|
|
return os.getenv(_ORIG_DIR) + '/' + path # type: ignore
|
|
|
|
else:
|
|
|
|
return path
|
|
|
|
|
2019-08-05 23:09:04 +00:00
|
|
|
|
|
|
|
def add_html(singleBlock=True, blockType='html'):
|
2019-12-19 10:34:19 +00:00
|
|
|
"""Create one-off web page from HTML file, no ext resources."""
|
|
|
|
add_file(blockType=blockType)
|
2019-08-05 23:09:04 +00:00
|
|
|
|
2019-09-21 05:06:49 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
add_html.onionr_help = "Adds an HTML file into Onionr. Does " # type: ignore
|
|
|
|
add_html.onionr_help += "not include dependant resources" # type: ignore
|
2019-03-08 01:08:06 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
|
|
|
|
def add_file(blockType='bin'):
|
|
|
|
"""Add a file to the onionr network."""
|
2019-03-08 01:08:06 +00:00
|
|
|
if len(sys.argv) >= 3:
|
|
|
|
filename = sys.argv[2]
|
2019-12-19 10:34:19 +00:00
|
|
|
|
2019-09-10 06:05:59 +00:00
|
|
|
if not os.path.exists(_get_dir(filename)):
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.error(
|
|
|
|
'That file does not exist. Improper path (specify full path)?',
|
|
|
|
terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
return
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info('Adding file, this might take a long time.',
|
|
|
|
terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
try:
|
2019-12-19 10:34:19 +00:00
|
|
|
with open(_get_dir(filename), 'rb') as single_file:
|
|
|
|
blockhash = insert(single_file.read(), header=blockType)
|
2019-03-08 01:08:06 +00:00
|
|
|
if len(blockhash) > 0:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info('File %s saved in block %s' %
|
|
|
|
(filename, blockhash), terminal=True)
|
|
|
|
except Exception as err: # pylint: disable=W0703
|
|
|
|
logger.error('Failed to save file in block ' +
|
|
|
|
str(err), timestamp=False, terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
else:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.error('%s add-file <filename>' %
|
|
|
|
sys.argv[0], timestamp=False, terminal=True)
|
|
|
|
|
|
|
|
|
|
|
|
add_file.onionr_help = "<file path> Add a file into " # type: ignore
|
|
|
|
add_file.onionr_help += "the Onionr network" # type: ignore
|
2019-03-09 01:57:44 +00:00
|
|
|
|
2019-09-21 05:06:49 +00:00
|
|
|
|
2019-08-05 23:09:04 +00:00
|
|
|
def get_file():
|
2019-12-19 10:34:19 +00:00
|
|
|
"""Get a file from onionr blocks."""
|
2019-03-09 01:57:44 +00:00
|
|
|
try:
|
2019-12-19 10:34:19 +00:00
|
|
|
file_name = _get_dir(sys.argv[2])
|
2019-03-09 01:57:44 +00:00
|
|
|
bHash = sys.argv[3]
|
|
|
|
except IndexError:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.error("Syntax %s %s" % (
|
|
|
|
sys.argv[0], '/path/to/filename <blockhash>'), terminal=True)
|
2019-03-09 01:57:44 +00:00
|
|
|
else:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info(file_name, terminal=True)
|
2019-03-09 01:57:44 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
if os.path.exists(file_name):
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.error("File already exists", terminal=True)
|
2019-03-09 01:57:44 +00:00
|
|
|
return
|
2019-06-25 23:07:35 +00:00
|
|
|
if not stringvalidators.validate_hash(bHash):
|
2019-06-20 00:59:05 +00:00
|
|
|
logger.error('Block hash is invalid', terminal=True)
|
2019-03-09 01:57:44 +00:00
|
|
|
return
|
|
|
|
|
2019-09-10 06:05:59 +00:00
|
|
|
try:
|
2019-12-19 10:34:19 +00:00
|
|
|
with open(file_name, 'wb') as my_file:
|
|
|
|
my_file.write(Block(bHash).bcontent)
|
2019-09-10 06:05:59 +00:00
|
|
|
except onionrexceptions.NoDataAvailable:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.error(
|
|
|
|
'That block is not available. Trying again later may work.',
|
|
|
|
terminal=True)
|
|
|
|
|
2019-09-21 05:06:49 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
get_file.onionr_help = "<file path> <block hash>: Download " # type: ignore
|
|
|
|
get_file.onionr_help += "a file from the onionr network." # type: ignore
|