|
| 1 | +""" |
| 2 | +File sharing endpoints (Bitswap / MerkleDag). |
| 3 | +
|
| 4 | +GET /api/v1/files/shared - list files this node has shared |
| 5 | +GET /api/v1/files/shared/{cid} - metadata for a specific shared file |
| 6 | +POST /api/v1/files/share - share a local file to a topic |
| 7 | +POST /api/v1/files/download - download a file by CID hex |
| 8 | +POST /api/v1/files/upload - upload via multipart and share to a topic |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +from .base import BaseHandler |
| 13 | + |
| 14 | + |
| 15 | +class SharedFilesHandler(BaseHandler): |
| 16 | + """GET /api/v1/files/shared""" |
| 17 | + |
| 18 | + def get(self): |
| 19 | + if not self.require_ready(): |
| 20 | + return |
| 21 | + files = [ |
| 22 | + {"cid": cid, **meta} |
| 23 | + for cid, meta in self.service.shared_files.items() |
| 24 | + ] |
| 25 | + self.send_success({"shared_files": files, "count": len(files)}) |
| 26 | + |
| 27 | + |
| 28 | +class SharedFileDetailHandler(BaseHandler): |
| 29 | + """GET /api/v1/files/shared/{cid}""" |
| 30 | + |
| 31 | + def get(self, cid): |
| 32 | + if not self.require_ready(): |
| 33 | + return |
| 34 | + meta = self.service.shared_files.get(cid) |
| 35 | + if not meta: |
| 36 | + self.send_error_response(f"No shared file with CID '{cid}'.", status=404) |
| 37 | + return |
| 38 | + self.send_success({"cid": cid, **meta}) |
| 39 | + |
| 40 | + |
| 41 | +class ShareFileHandler(BaseHandler): |
| 42 | + """POST /api/v1/files/share — share a file that already exists on disk""" |
| 43 | + |
| 44 | + def post(self): |
| 45 | + if not self.require_ready(): |
| 46 | + return |
| 47 | + body = self.get_json_body() |
| 48 | + file_path = body.get("file_path", "").strip() |
| 49 | + topic = body.get("topic", "").strip() |
| 50 | + |
| 51 | + if not file_path: |
| 52 | + self.send_error_response("'file_path' is required.") |
| 53 | + return |
| 54 | + if not topic: |
| 55 | + self.send_error_response("'topic' is required.") |
| 56 | + return |
| 57 | + if not os.path.exists(file_path): |
| 58 | + self.send_error_response(f"File not found: {file_path}", status=400) |
| 59 | + return |
| 60 | + |
| 61 | + subscribed = self.service.get_subscribed_topics() |
| 62 | + if topic not in subscribed: |
| 63 | + self.send_error_response( |
| 64 | + f"Not subscribed to topic '{topic}'. Subscribe first via POST /api/v1/topics.", |
| 65 | + status=400, |
| 66 | + ) |
| 67 | + return |
| 68 | + |
| 69 | + queued = self.service.share_file(file_path, topic) |
| 70 | + if queued: |
| 71 | + filename = os.path.basename(file_path) |
| 72 | + self.send_success( |
| 73 | + {"message": "File share request queued", "filename": filename, "topic": topic}, |
| 74 | + status=202, |
| 75 | + ) |
| 76 | + else: |
| 77 | + self.send_error_response("Failed to queue file share — service not ready.", status=503) |
| 78 | + |
| 79 | + |
| 80 | +class DownloadFileHandler(BaseHandler): |
| 81 | + """POST /api/v1/files/download — download a file by CID hex""" |
| 82 | + |
| 83 | + def post(self): |
| 84 | + if not self.require_ready(): |
| 85 | + return |
| 86 | + body = self.get_json_body() |
| 87 | + cid = body.get("file_cid", "").strip() |
| 88 | + name = body.get("file_name", "unknown").strip() |
| 89 | + |
| 90 | + if not cid: |
| 91 | + self.send_error_response("'file_cid' is required.") |
| 92 | + return |
| 93 | + |
| 94 | + queued = self.service.download_file(cid, name) |
| 95 | + if queued: |
| 96 | + self.send_success( |
| 97 | + {"message": "Download request queued", "file_cid": cid, "file_name": name}, |
| 98 | + status=202, |
| 99 | + ) |
| 100 | + else: |
| 101 | + self.send_error_response("Failed to queue download — service not ready.", status=503) |
| 102 | + |
| 103 | + |
| 104 | +class UploadAndShareHandler(BaseHandler): |
| 105 | + """ |
| 106 | + POST /api/v1/files/upload |
| 107 | + Accepts multipart/form-data with fields: |
| 108 | + - file : the file bytes |
| 109 | + - topic : the topic to share to |
| 110 | + Saves the file to the service's download_dir and queues a share. |
| 111 | + """ |
| 112 | + |
| 113 | + def post(self): |
| 114 | + if not self.require_ready(): |
| 115 | + return |
| 116 | + |
| 117 | + topic = self.get_argument("topic", "").strip() |
| 118 | + if not topic: |
| 119 | + self.send_error_response("'topic' form field is required.") |
| 120 | + return |
| 121 | + |
| 122 | + if "file" not in self.request.files: |
| 123 | + self.send_error_response("'file' form-data field is required.") |
| 124 | + return |
| 125 | + |
| 126 | + file_info = self.request.files["file"][0] |
| 127 | + filename = file_info["filename"] or "upload" |
| 128 | + file_data = file_info["body"] |
| 129 | + |
| 130 | + # Save to download_dir |
| 131 | + save_path = os.path.join(self.service.download_dir, filename) |
| 132 | + # Handle name collisions |
| 133 | + counter = 1 |
| 134 | + base, ext = os.path.splitext(filename) |
| 135 | + while os.path.exists(save_path): |
| 136 | + save_path = os.path.join(self.service.download_dir, f"{base}_{counter}{ext}") |
| 137 | + counter += 1 |
| 138 | + |
| 139 | + with open(save_path, "wb") as f: |
| 140 | + f.write(file_data) |
| 141 | + |
| 142 | + subscribed = self.service.get_subscribed_topics() |
| 143 | + if topic not in subscribed: |
| 144 | + self.send_error_response( |
| 145 | + f"Not subscribed to topic '{topic}'. Subscribe first via POST /api/v1/topics.", |
| 146 | + status=400, |
| 147 | + ) |
| 148 | + return |
| 149 | + |
| 150 | + queued = self.service.share_file(save_path, topic) |
| 151 | + if queued: |
| 152 | + self.send_success( |
| 153 | + { |
| 154 | + "message": "File uploaded and share request queued", |
| 155 | + "filename": os.path.basename(save_path), |
| 156 | + "size": len(file_data), |
| 157 | + "topic": topic, |
| 158 | + "saved_path": save_path, |
| 159 | + }, |
| 160 | + status=202, |
| 161 | + ) |
| 162 | + else: |
| 163 | + self.send_error_response("Failed to queue file share — service not ready.", status=503) |
0 commit comments