def test_upload_file_custom_filename(): """Test uploading a file with a custom filename.""" src_file = './testdata/file1' skylink = 'testskylinkcustomname' sialink = Skynet.uri_skynet_prefix() + skylink custom_name = 'testname' opts = Skynet.default_upload_options() opts.custom_filename = custom_name # upload a file with custom filename responses.add(responses.POST, 'https://siasky.net/skynet/skyfile', json={'skylink': skylink}, status=200) print("Uploading file " + src_file) sialink2 = Skynet.upload_file(src_file, opts) if sialink != sialink2: sys.exit("ERROR: expected returned sialink " + sialink + ", received " + sialink2) print("File upload successful, sialink: " + sialink2) body = responses.calls[0].request.body assert str(body).find('Content-Disposition: form-data; name="file"; \ filename="' + custom_name + '"') != -1 with open(src_file, 'r') as fd: contents = fd.read().strip() assert str(body).find(contents) != -1 assert len(responses.calls) == 1
def sky_download(filen,skylink): try: Skynet.download_file(filen,upload_f.skylink) print("Downloaded!") except: print("Skylink does not exist or could not be generated.")
def test_upload_directory(): """Test uploading a directory.""" src_dir = './testdata' skylink = 'testskylink' sialink = Skynet.uri_skynet_prefix() + skylink # upload a directory responses.add(responses.POST, 'https://siasky.net/skynet/skyfile', json={'skylink': skylink}, status=200) print('Uploading dir ' + src_dir) sialink2 = Skynet.upload_directory(src_dir) if sialink != sialink2: sys.exit('ERROR: expected returned sialink ' + sialink + ', received ' + sialink2) print('Dir upload successful, sialink: ' + sialink) body = responses.calls[0].request.body assert str(body).find('Content-Disposition: form-data; name="files[]"; \ filename="file1"') != -1 assert str(body).find('Content-Disposition: form-data; name="files[]"; \ filename="file3"') != -1 assert str(body).find('Content-Disposition: form-data; name="files[]"; \ filename="dir1/file2"') != -1 # Check a file that shouldn't be there. assert str(body).find('Content-Disposition: form-data; name="files[]"; \ filename="file0"') == -1 assert len(responses.calls) == 1
async def post(self): import zipfile from requests.auth import HTTPBasicAuth from siaskynet import Skynet uploaded_file = self.request.files['file'][0] filename = self.get_query_argument('filename') local_filename = '/tmp/{}'.format(filename) with open(uploaded_file['filename'], 'wb') as f: f.write(uploaded_file['body']) with zipfile.ZipFile(uploaded_file['filename'], 'r') as zip_ref: zip_ref.extractall(local_filename + '/dir') try: opts = Skynet.default_upload_options() opts.portal_url = 'http://0.0.0.0:9980' skylink = Skynet.upload_directory(local_filename + '/dir') except Exception as e: self.set_status(400) return self.render_as_json({ 'status': 'error', 'message': 'sia node not responding' }) return self.render_as_json({ 'status': 'success', 'skylink': Skynet.strip_prefix(skylink) })
def upload(file): # upload file to skynet print('uploading to skynet') opts = Skynet.default_upload_options opts.portalUrl = uploadportal skylink = Skynet.UploadFile(file) # put link into clipboard pyperclip.copy(linkportal + '/' + Skynet.strip_prefix(skylink)) print('upload finished') # beep playsound(beepname, False) # wait for sound to play then close sleep(3)
def main(): #coder A = encode() #upload image skylink = Skynet.upload_file(A) print("Upload successful, skylink: " + skylink) #download image Skynet.download_file("./dest.png", skylink) print("Download successful") # encoder Image print("Decoded word- " + decode("./dest.png"))
async def skyup(ctx, *args): try: ts = datetime.now().strftime("-%H:%M:%S:%f") if ctx.message.attachments: url = ctx.message.attachments[0].url r = requests.get(url) file_name = url.split('/')[-1] + ts with open(file_name, 'wb') as new_file: new_file.write(r.content) else: if not args: return file_name = 'text_message.txt' + ts with open(file_name, 'wt') as new_file: new_file.write(ctx.message.content[7:]) opts = Skynet.default_upload_options() opts.custom_filename = file_name[:-16] skylink = upload_file(file_name, opts) embed = Embed(title='Upload successful') embed.add_field(name='Sia link', value=skylink, inline=False) embed.add_field(name='Web link', value=skylink.replace('sia://', 'https://siasky.net/'), inline=False) embed.add_field(name='Requested by', value=ctx.message.author.mention, inline=False) await ctx.send(embed=embed) new_file.close() os.remove(file_name) except Exception as ex: traceback.print_exc() logging.error('Exception of type {%s}: {%s}' % (type(ex).__name__, str(ex)))
def download_and_upload(image_url): filename = image_url.split("/")[-1] filename = filename.split('?')[0] # Open the url image, set stream to True, this will return the stream content. r = requests.get(image_url, stream=True) # Check if the image was retrieved successfully if r.status_code == 200: # Set decode_content value to True, otherwise the downloaded image file's size will be zero. r.raw.decode_content = True # Open a local file with wb ( write binary ) permission. with open(filename, 'wb') as f: shutil.copyfileobj(r.raw, f) print('Image sucessfully Downloaded: ', filename) skylink = Skynet.upload_file(filename) if skylink is not None: os.remove(filename) return skylink else: print('Image Couldn\'t be retreived') return None
def handle_message(message): try: ts = datetime.now().strftime("%H:%M:%S.%f") if (message.content_type == 'text'): file_name = 'text_message-' + ts + '.txt' with open(file_name, 'wt') as new_file: new_file.write(message.text) else: if (message.content_type == 'photo'): file_info = bot.get_file(message.photo[0].file_id) else: file_info = bot.get_file(getattr(message, message.content_type).file_id) downloaded_file = bot.download_file(file_info.file_path) file_name = './' + message.content_type + ts with open(file_name, 'wb') as new_file: new_file.write(downloaded_file) skylink = Skynet.upload_file(file_name) bot.reply_to(message, 'Upload successful\n\n<b>Sia link:</b> <code>%s</code>\n\n<b>Web link:</b> %s' % (skylink, skylink.replace('sia://', 'https://siasky.net/')), parse_mode='HTML', disable_web_page_preview=True) new_file.close() os.remove(file_name) except Exception as ex: traceback.print_exc() logging.error('Exception of type {%s}: {%s}' % (type(ex).__name__, str(ex)))
def test_upload_and_download_file(): """Test uploading and download a file.""" src_file = "./testdata/file1" skylink = 'testskylink' sialink = Skynet.uri_skynet_prefix() + skylink # upload a file responses.add(responses.POST, 'https://siasky.net/skynet/skyfile', json={'skylink': skylink}, status=200) print("Uploading file " + src_file) sialink2 = Skynet.upload_file(src_file) if sialink != sialink2: sys.exit("ERROR: expected returned sialink " + sialink + ", received " + sialink2) print("File upload successful, sialink: " + sialink2) body = responses.calls[0].request.body assert str(body).find('Content-Disposition: form-data; name="file"; \ filename="file1"') != -1 with open(src_file, 'r') as fd: contents = fd.read().strip() assert str(body).find(contents) != -1 # download a file responses.add(responses.GET, 'https://siasky.net/' + skylink, "test\n", status=200) dst_file = tempfile.NamedTemporaryFile().name print("Downloading to " + dst_file) Skynet.download_file(dst_file, skylink) if not filecmp.cmp(src_file, dst_file): sys.exit("ERROR: Downloaded file at " + dst_file + " did not equal uploaded file " + src_file) print("File download successful") assert len(responses.calls) == 2
def sia_upload_options(custom_filename='', portal=None): options = Skynet.default_upload_options() if portal: options.portal_url = SIA_API_BASEPATH options.custom = True else: options.custom = False options.custom_filename = custom_filename return options
def upload_file_request(path, opts=None): if opts is None: opts = Skynet.default_upload_options() with open(path, 'rb') as f: host = opts.portal_url path = opts.portal_upload_path url = f'{host}/{path}' filename = opts.custom_filename if opts.custom_filename else os.path.basename( f.name) r = requests.post(url, files={opts.portal_file_fieldname: (filename, f)}) return r
def upload_image(): if request.method == "POST": if request.files: if "filesize" in request.cookies: if not allowed_image_filesize(request.cookies["filesize"]): print("Filesize exceeded maximum limit") return redirect(request.url) image = request.files["image"] if image == "": print("No filename") return redirect(request.url) if allowed_image(image.filename): filename = secure_filename(image.filename) image.save( os.path.join(app.config["IMAGE_UPLOADS"], filename)) print("Image saved") #print(os.path.join(os.environ["PATH"], filename)) skylink = Skynet.upload_file( os.path.join(app.config["IMAGE_UPLOADS"], filename)) #print("Upload successful, skylink: " + skylink) #os.remove(filename) global imagelink imagelink = 'https://siasky.net/' + skylink[6:] f = open('data.txt', 'w+') f.write(imagelink + '\n') f.close() localisation = request.get_json() print(imagelink) print(localisation) #skylink = Skynet.upload_file(os.path.join(app.config["IMAGE_UPLOADS"], filename)) #skylink = Skynet.upload_file(os.path.abspath(os.getcwd())) return redirect(request.url) #return redirect(url_for('upload_image', **locals())) else: print("That file extension is not allowed") return redirect(request.url) return render_template("public/upload_image.html", skylink=imagelink)
def post_upload_file(filename): options = Skynet.default_upload_options() host = options.portal_url path = options.portal_upload_path url = f'{host}/{path}' res = requests.get(filename) filename = filename.split("/")[-1] f = open(filename, "w+") f.write(res.text) f.close() f = open(filename, "rb") r = requests.post(url, files={options.portal_file_fieldname: (filename, f)}) f.close() return r
def upload_song(): if request.method == 'POST': logging.debug('if request.method == POST: is TRUE') file = request.files['file'] if file.filename: filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) skylink = Skynet.upload_file( os.path.join(app.config["UPLOAD_FOLDER"], filename)) sizez = skylink[skylink.startswith("sia:/") and len("sia:/"):] our_url = "https://siasky.net" + sizez print("Image saved : " + our_url) return render_template('index.html', filename=filename, our_url=our_url) return render_template('index.html')
def skynet_push(filePath, portal): logging.debug('Uploading ' + str(filePath) + ' with ' + str(portal)) opts = type('obj', (object, ), { 'portal_url': portal, 'timeout': 60, 'timeout_seconds': 60 }) try: try: return Skynet.upload_file(filePath, opts) except TimeoutError: logging.error('Uploading timeout with ' + str(portal)) return False except: logging.error('Uploading failed with ' + str(portal)) return False
def upload_file_request(path, opts=None, force=False): if opts.custom: with open(os.path.abspath('.apipassword')) as f: password = f.read().strip() host = opts.portal_url portal_path = opts.portal_upload_path remote_path = path.split('cache/')[1] url = f'{host}/{portal_path}/{remote_path}?filename={opts.custom_filename}' if force: url += '&force=true' with open(path, 'rb') as f: r = requests.post(url, headers=headers, data=f.read(), auth=('', password)) return r return Skynet.upload_file_request(path, opts)
def handle_message(**payload): data = payload['data'] web_client = payload['web_client'] rtm_client = payload['rtm_client'] text = None if 'text' in data: channel_id = data['channel'] thread_ts = data['ts'] user = data['user'] text = data.get('text', []) match = re.match(MENTION_REGEX, text) if match: user_id, message = match.groups() message = message.strip(" ") else: message = text message = message.replace(u'\xa0', u' ') split_message = message.split(" ") command = split_message.pop(0) files = split_message files_options = { "token": os.environ['SLACK_BOT_TOKEN'], "channel": channel_id, "user": user, } #res = requests.get('https://slack.com/api/files.list', files_options) #print(res.text) res = requests.get(files[0][1:-1]) if command in acceptable_commands: options = Skynet.default_upload_options() #for filename in files: filename = files[0][1:-1] skylink = upload_file(filename, options) response = web_client.chat_postMessage( channel=channel_id, text=str(skylink), thread_ts=thread_ts )
def create_gist(request): gist_id = uuid.uuid1().hex files = request.data.get('files') raw_metadata = request.data.get('metadata') repo_path = path.join(SIA_CACHE_DIR, gist_id) repo = Repo.init(repo_path) parent = raw_metadata.get('parent', None), parent_gist = None if parent: gist = Gist.objects.filter(sia_path=parent) if gist.exists(): parent_gist = gist.first() base_metadata = { 'visibility': raw_metadata.get('visibility', 'public'), 'parent': parent_gist, 'description': raw_metadata.get('description', ''), 'expiration': raw_metadata.get('expiration', 'never'), 'categories': raw_metadata.get('categories', []), 'user_address': raw_metadata.get('address', ''), 'uuid': gist_id } params = base_metadata.copy() params['sia_path'] = f'{gist_id}.zip' params['skynet_url'] = '' new_gist = Gist.objects.create(**params) metadata = base_metadata.copy() metadata['sia_package_path'] = f'{gist_id}.zip' metadata['skynet_package_url'] = '' metadata['files'] = { file['name']: { 'syntax': file['syntax'], 'skynet_url': '' } for file in files } metadata['created'] = timezone.now().isoformat() # Write and commit files [filepaths, metadata_path] = write_files(gist_id, files, metadata) paths = [ path.join(SIA_CACHE_DIR, filepath['path']) for filepath in filepaths ] metadata_abs_path = path.join(SIA_CACHE_DIR, metadata_path) repo.index.add([metadata_abs_path] + paths) repo.index.commit('Initial revision') # Set manifest new_gist.skynet_manifest_url = Skynet.upload_file( metadata_abs_path, custom_filename=f'{gist_id}.json') new_gist.sia_manifest_path = metadata_path new_gist.save() # Upload files for filepath in filepaths: print(f'======= {filepath}') filename = filepath['path'].split(f'{gist_id}/')[1] print(f'======= {filename}') file = File.objects.create(sia_path=filepath['path'], skynet_url='', file_name=filename, gist=new_gist, syntax=filepath['syntax']) upload_file(gist_id=new_gist.id, file_id=file.id, filename=filename) upload_zip.delay(gist_id=new_gist.id, filename=f'{gist_id}.zip') return Response({ 'id': gist_id, 'url': f'{HOST_URL}/gists/{gist_id}', 'gist': new_gist.get_default_dict() })
'https://skyportal.xyz', 'https://skynet.cloudloop.io', 'https://siacdn.com', 'https://siasky.net', 'https://germany.siasky.net', 'https://helsinki.siasky.net', 'https://us-west.siasky.net/', ] results = [] for portal in portals: start_time = time.time() opts = type('obj', (object, ), {'portal_url': portal, 'timeout': 20}) try: try: skylink = Skynet.upload_file('2MBfile.txt', opts) uploadtime = round(time.time() - start_time, 2) current_result = [uploadtime, portal] except TimeoutError as e: current_result = [opts.timeout, portal] except Exception as e: current_result = [999, portal] results.append(current_result) print('Benchmarking', str(len(results)) + '/' + str(len(portals)), 'portal. Current:', time_to_str(current_result[0]), current_result[1]) print('\nRESULTS:\n') results.sort(key=lambda x: x[0]) for elem in results:
def perform_create(self, serializer): temp_file = self.request.data["file"] skylink = Skynet.UploadFile(temp_file) serializer.save(file=skylink, owner=self.request.user)
def upload_f(path): skylink = Skynet.upload_file(path) return skylink
def upload_file(path, custom_filename='', portal=None, force=False): opts = sia_upload_options(custom_filename, portal) resp = SkynetClient.upload_file_request(path, opts, force).json() print(resp) return Skynet.uri_skynet_prefix() + resp["skylink"]
from siaskynet import Skynet import json # upload skylink = Skynet.UploadFile("img-data.txt") print("Upload successful, skylink: " + skylink.replace("sia://", "https://siasky.net/")) with open("../../address_book.json") as f: data = f.read() f.close() # write to values inside addressbook new_file = open("../../address_book.json", "w") new_file.write(data.replace("HAVE NOT FILLED IN NEW ADDRESS FROM UPLOAD.PY", skylink.replace("sia://", "https://siasky.net/"))) print("Successfully changed last address\n")
def upload_file(path, opts=None): return Skynet.uri_skynet_prefix() + upload_file_request( path, opts).json()["skylink"]