|
| 1 | +# Pod2Post Write Text API |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Writes text content to a file and optionally uploads it to OSS, returning a downloadable URL. |
| 6 | + |
| 7 | +## Endpoint |
| 8 | + |
| 9 | +**POST** `/api/generate/pod2post/write-text` |
| 10 | + |
| 11 | +## Request |
| 12 | + |
| 13 | +### Headers |
| 14 | +``` |
| 15 | +Content-Type: application/json |
| 16 | +``` |
| 17 | + |
| 18 | +### Body Parameters |
| 19 | + |
| 20 | +| Parameter | Type | Required | Default | Description | |
| 21 | +|-----------|------|----------|---------|-------------| |
| 22 | +| task_id | string | Yes | - | Task ID in format: `pod2post_{timestamp}_{random}` | |
| 23 | +| filename | string | Yes | - | Filename with extension (e.g., `content.json`) | |
| 24 | +| content | string | Yes | - | Text content (max 10MB) | |
| 25 | +| token | string | No | - | User authentication token | |
| 26 | +| upload_to_oss | boolean | No | true | Whether to upload to OSS | |
| 27 | +| return_oss_url | boolean | No | true | Whether to return OSS download URL | |
| 28 | + |
| 29 | +### Example Request |
| 30 | + |
| 31 | +```json |
| 32 | +{ |
| 33 | + "task_id": "pod2post_1736781000_abc123", |
| 34 | + "filename": "content.json", |
| 35 | + "content": "{\"title\": \"Article Title\", \"content\": \"Article content...\"}", |
| 36 | + "upload_to_oss": true, |
| 37 | + "return_oss_url": true |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +## Response |
| 42 | + |
| 43 | +### Success Response (200 OK) |
| 44 | + |
| 45 | +```json |
| 46 | +{ |
| 47 | + "code": 200, |
| 48 | + "success": true, |
| 49 | + "message": "File written successfully and download link generated", |
| 50 | + "data": { |
| 51 | + "filename": "content.json", |
| 52 | + "path": "/app/data/users/default/workspace/card/pod2post_1736781000_abc123/content.json", |
| 53 | + "size": 1024, |
| 54 | + "isNew": true, |
| 55 | + "taskId": "pod2post_1736781000_abc123", |
| 56 | + "username": "default", |
| 57 | + "createdAt": "2025-01-13T10:30:00.000Z", |
| 58 | + "modifiedAt": "2025-01-13T10:30:00.000Z", |
| 59 | + "downloadUrl": "https://cms-mcp.oss-cn-hangzhou.aliyuncs.com/pod2post/default/pod2post_1736781000_abc123/content.json?OSSAccessKeyId=LTAI5tP7iEeXDKDgc8B1GWeW&Expires=2075711157&Signature=xxx", |
| 60 | + "ossPath": "pod2post/default/pod2post_1736781000_abc123/content.json", |
| 61 | + "ossUrl": "https://cms-mcp.oss-cn-hangzhou.aliyuncs.com/default/default/content.json", |
| 62 | + "oss": { |
| 63 | + "ossPath": "pod2post/default/pod2post_1736781000_abc123/content.json", |
| 64 | + "ossUrl": "https://cms-mcp.oss-cn-hangzhou.aliyuncs.com/default/default/content.json", |
| 65 | + "downloadUrl": "https://cms-mcp.oss-cn-hangzhou.aliyuncs.com/pod2post/default/pod2post_1736781000_abc123/content.json?OSSAccessKeyId=LTAI5tP7iEeXDKDgc8B1GWeW&Expires=2075711157&Signature=xxx", |
| 66 | + "uploadedAt": "2025-01-13T10:30:05.000Z" |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Error Responses |
| 73 | + |
| 74 | +#### 400 Bad Request |
| 75 | +```json |
| 76 | +{ |
| 77 | + "code": 400, |
| 78 | + "success": false, |
| 79 | + "message": "Invalid parameter: task_id format is incorrect, should be pod2post_{timestamp}_{random}" |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +#### 500 Internal Server Error |
| 84 | +```json |
| 85 | +{ |
| 86 | + "code": 500, |
| 87 | + "success": false, |
| 88 | + "message": "File write failed" |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Response Fields |
| 93 | + |
| 94 | +| Field | Type | Description | |
| 95 | +|-------|------|-------------| |
| 96 | +| filename | string | The filename | |
| 97 | +| path | string | Local file path | |
| 98 | +| size | number | File size in bytes | |
| 99 | +| isNew | boolean | Whether the file is newly created | |
| 100 | +| taskId | string | Task ID | |
| 101 | +| username | string | Username | |
| 102 | +| downloadUrl | string | OSS download URL (if uploaded to OSS) | |
| 103 | +| ossPath | string | OSS storage path | |
| 104 | +| ossUrl | string | OSS base URL | |
| 105 | +| oss | object | OSS detailed information (optional) | |
| 106 | + |
| 107 | +## Usage Examples |
| 108 | + |
| 109 | +### JavaScript/TypeScript |
| 110 | + |
| 111 | +```javascript |
| 112 | +const writeTextFile = async (taskId, filename, content) => { |
| 113 | + const response = await fetch('/api/generate/pod2post/write-text', { |
| 114 | + method: 'POST', |
| 115 | + headers: { |
| 116 | + 'Content-Type': 'application/json' |
| 117 | + }, |
| 118 | + body: JSON.stringify({ |
| 119 | + task_id: taskId, |
| 120 | + filename: filename, |
| 121 | + content: content |
| 122 | + }) |
| 123 | + }); |
| 124 | + |
| 125 | + const result = await response.json(); |
| 126 | + |
| 127 | + if (result.success) { |
| 128 | + return result.data.downloadUrl; // Returns OSS download URL |
| 129 | + } else { |
| 130 | + throw new Error(result.message); |
| 131 | + } |
| 132 | +}; |
| 133 | + |
| 134 | +// Example usage |
| 135 | +const taskId = `pod2post_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; |
| 136 | +const content = JSON.stringify({ |
| 137 | + title: "My Document", |
| 138 | + content: "This is the document content", |
| 139 | + timestamp: new Date().toISOString() |
| 140 | +}); |
| 141 | + |
| 142 | +try { |
| 143 | + const downloadUrl = await writeTextFile(taskId, "document.json", content); |
| 144 | + console.log("Download URL:", downloadUrl); |
| 145 | + // You can now use this URL to download the file |
| 146 | + window.open(downloadUrl); |
| 147 | +} catch (error) { |
| 148 | + console.error("Error:", error.message); |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### cURL |
| 153 | + |
| 154 | +```bash |
| 155 | +curl -X POST http://localhost:8098/api/generate/pod2post/write-text \ |
| 156 | + -H "Content-Type: application/json" \ |
| 157 | + -d '{ |
| 158 | + "task_id": "pod2post_1736781000_abc123", |
| 159 | + "filename": "data.json", |
| 160 | + "content": "{\"message\": \"Hello World\"}" |
| 161 | + }' |
| 162 | +``` |
| 163 | + |
| 164 | +### Python |
| 165 | + |
| 166 | +```python |
| 167 | +import requests |
| 168 | +import json |
| 169 | +import time |
| 170 | +import random |
| 171 | +import string |
| 172 | + |
| 173 | +def write_text_to_pod2post(filename, content, token=None): |
| 174 | + # Generate task ID |
| 175 | + timestamp = int(time.time()) |
| 176 | + random_str = ''.join(random.choices(string.ascii_lowercase + string.digits, k=9)) |
| 177 | + task_id = f"pod2post_{timestamp}_{random_str}" |
| 178 | + |
| 179 | + url = "http://localhost:8098/api/generate/pod2post/write-text" |
| 180 | + |
| 181 | + payload = { |
| 182 | + "task_id": task_id, |
| 183 | + "filename": filename, |
| 184 | + "content": content |
| 185 | + } |
| 186 | + |
| 187 | + if token: |
| 188 | + payload["token"] = token |
| 189 | + |
| 190 | + response = requests.post(url, json=payload) |
| 191 | + result = response.json() |
| 192 | + |
| 193 | + if result["success"]: |
| 194 | + return result["data"]["downloadUrl"] |
| 195 | + else: |
| 196 | + raise Exception(result["message"]) |
| 197 | + |
| 198 | +# Usage |
| 199 | +content = json.dumps({ |
| 200 | + "title": "Python Example", |
| 201 | + "data": [1, 2, 3, 4, 5] |
| 202 | +}) |
| 203 | + |
| 204 | +try: |
| 205 | + download_url = write_text_to_pod2post("example.json", content) |
| 206 | + print(f"Download URL: {download_url}") |
| 207 | +except Exception as e: |
| 208 | + print(f"Error: {e}") |
| 209 | +``` |
| 210 | + |
| 211 | +## Notes |
| 212 | + |
| 213 | +1. **Download URL**: The `downloadUrl` field contains a signed URL valid for 10 years |
| 214 | +2. **Task ID Format**: Must start with `pod2post_` followed by timestamp and random characters |
| 215 | +3. **File Extensions**: Must include file extension (.json, .txt, .md, .html, etc.) |
| 216 | +4. **Content Limit**: Maximum content size is 10MB |
| 217 | +5. **Error Handling**: OSS upload failure doesn't affect local file write success |
| 218 | +6. **Supported File Types**: .txt, .json, .md, .html, .css, .js, .xml, .csv, .pdf, .doc, .docx, etc. |
| 219 | + |
| 220 | +## SDK/Helper Functions |
| 221 | + |
| 222 | +### JavaScript |
| 223 | + |
| 224 | +```javascript |
| 225 | +// Generate task ID helper |
| 226 | +function generateTaskId() { |
| 227 | + const timestamp = Date.now(); |
| 228 | + const random = Math.random().toString(36).substring(2, 9); |
| 229 | + return `pod2post_${timestamp}_${random}`; |
| 230 | +} |
| 231 | + |
| 232 | +// Write file wrapper |
| 233 | +async function writeFileWithOSS(filename, content, options = {}) { |
| 234 | + const defaultOptions = { |
| 235 | + upload_to_oss: true, |
| 236 | + return_oss_url: true |
| 237 | + }; |
| 238 | + |
| 239 | + const payload = { |
| 240 | + task_id: generateTaskId(), |
| 241 | + filename, |
| 242 | + content, |
| 243 | + ...defaultOptions, |
| 244 | + ...options |
| 245 | + }; |
| 246 | + |
| 247 | + const response = await fetch('/api/generate/pod2post/write-text', { |
| 248 | + method: 'POST', |
| 249 | + headers: { 'Content-Type': 'application/json' }, |
| 250 | + body: JSON.stringify(payload) |
| 251 | + }); |
| 252 | + |
| 253 | + return response.json(); |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +## Rate Limits |
| 258 | + |
| 259 | +- No explicit rate limits |
| 260 | +- Content size limited to 10MB per request |
| 261 | +- File size limited to 50MB for uploaded files |
| 262 | + |
| 263 | +## Version History |
| 264 | + |
| 265 | +- **v1.1.0** (2025-01-13): Added OSS upload functionality |
| 266 | +- **v1.0.0** (2025-10-11): Initial release with local file write only |
0 commit comments