added file reader offset
This commit is contained in:
		
							parent
							
								
									723d004e5c
								
							
						
					
					
						commit
						c23b286540
					
				
					 5 changed files with 80 additions and 2 deletions
				
			
		|  | @ -9,6 +9,7 @@ from httpapi import security, friendsapi, profilesapi, configapi, insertblock | |||
| from httpapi import miscclientapi, onionrsitesapi, apiutils | ||||
| from httpapi import directconnections | ||||
| from httpapi import themeapi | ||||
| from httpapi import fileoffsetreader | ||||
| from httpapi.sse.private import private_sse_blueprint | ||||
| 
 | ||||
| """ | ||||
|  | @ -46,6 +47,7 @@ def register_private_blueprints(private_api, app): | |||
|         private_api).direct_conn_management_bp) | ||||
|     app.register_blueprint(themeapi.theme_blueprint) | ||||
|     app.register_blueprint(private_sse_blueprint) | ||||
|     app.register_blueprint(fileoffsetreader.offset_reader_api) | ||||
| 
 | ||||
|     def _add_events_bp(): | ||||
|         while True: | ||||
|  |  | |||
							
								
								
									
										30
									
								
								src/httpapi/fileoffsetreader/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/httpapi/fileoffsetreader/__init__.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,30 @@ | |||
| from os.path import exists, dirname | ||||
| 
 | ||||
| import ujson | ||||
| from flask import Blueprint, Response, request | ||||
| 
 | ||||
| from utils.identifyhome import identify_home | ||||
| from utils.readoffset import read_from_offset | ||||
| 
 | ||||
| offset_reader_api = Blueprint('offsetreaderapi', __name__) | ||||
| 
 | ||||
| 
 | ||||
| @offset_reader_api.route('/readfileoffset/<name>') | ||||
| def offset_reader_endpoint(name): | ||||
|     if not name[:-4].isalnum(): | ||||
|         return Response(400, "Path must be alphanumeric except for file ext") | ||||
| 
 | ||||
|     path = identify_home() + name | ||||
| 
 | ||||
|     if not exists(path): | ||||
|         return Response(404, "Path not found in Onionr data directory") | ||||
| 
 | ||||
|     offset = request.args.get('offset') | ||||
| 
 | ||||
|     if not offset: | ||||
|         offset = 0 | ||||
|     else: | ||||
|         offset = int(offset) | ||||
|     result = read_from_offset(path, offset)._asdict() | ||||
| 
 | ||||
|     return ujson.dumps(result, reject_bytes=False) | ||||
							
								
								
									
										17
									
								
								src/utils/readoffset.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/utils/readoffset.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,17 @@ | |||
| """Onionr - Private P2P Communication. | ||||
| 
 | ||||
| read from a file from an offset (efficiently) | ||||
| """ | ||||
| from collections import namedtuple | ||||
| 
 | ||||
| OffsetReadResult = namedtuple('OffsetReadResult', ['data', 'new_offset']) | ||||
| 
 | ||||
| 
 | ||||
| def read_from_offset(file_path, offset=0): | ||||
|     with open(file_path, 'rb') as f: | ||||
|         if offset: | ||||
|             f.seek(offset) | ||||
|         data = f.read() | ||||
|         offset = f.tell() | ||||
| 
 | ||||
|     return OffsetReadResult(data, offset) | ||||
|  | @ -20,11 +20,11 @@ import os | |||
| """ | ||||
| 
 | ||||
| 
 | ||||
| def get_static_dir()->str: | ||||
| def get_static_dir() -> str: | ||||
|     return os.path.dirname(os.path.realpath(__file__)) + '/../../static-data/' | ||||
| 
 | ||||
| 
 | ||||
| def read_static(file:str, ret_bin:bool=False)->Union[str, bytes]: | ||||
| def read_static(file: str, ret_bin: bool = False) -> Union[str, bytes]: | ||||
|     static_file = get_static_dir() + file | ||||
| 
 | ||||
|     if ret_bin: | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue