MCPcopy Create free account
hub / github.com/pollinations/pollinations / commit_image_to_branch

Function commit_image_to_branch

social/scripts/common.py:454–496  ·  view source on GitHub ↗

Commit an image file to a GitHub branch and return a raw URL. Returns the raw.githubusercontent.com URL on success, None on failure.

(
    image_bytes: bytes,
    file_path: str,
    branch: str,
    github_token: str,
    owner: str,
    repo: str,
)

Source from the content-addressed store, hash-verified

452
453
454def commit_image_to_branch(
455 image_bytes: bytes,
456 file_path: str,
457 branch: str,
458 github_token: str,
459 owner: str,
460 repo: str,
461) -> Optional[str]:
462 """Commit an image file to a GitHub branch and return a raw URL.
463
464 Returns the raw.githubusercontent.com URL on success, None on failure.
465 """
466 import base64 as _b64
467
468 headers = _github_headers(github_token)
469
470 encoded = _b64.b64encode(image_bytes).decode()
471
472 # Check if file already exists on this branch
473 sha = get_file_sha(github_token, owner, repo, file_path, branch)
474
475 payload = {
476 "message": f"add image {file_path.split('/')[-1]}",
477 "content": encoded,
478 "branch": branch,
479 }
480 if sha:
481 payload["sha"] = sha
482
483 resp = github_api_request(
484 "PUT",
485 f"{GITHUB_API_BASE}/repos/{owner}/{repo}/contents/{file_path}",
486 headers=headers,
487 json=payload,
488 )
489
490 if resp.status_code in [200, 201]:
491 raw_url = f"https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{file_path}"
492 print(f" Committed image to {file_path}")
493 return raw_url
494
495 print(f" Failed to commit image: {resp.status_code} {resp.text[:200]}")
496 return None
497
498
499def get_file_sha(github_token: str, owner: str, repo: str, file_path: str, branch: str = "main") -> str:

Callers 3

generate_platform_imagesFunction · 0.90
generate_platform_imagesFunction · 0.90
generate_gist_imageFunction · 0.90

Calls 3

_github_headersFunction · 0.85
get_file_shaFunction · 0.85
github_api_requestFunction · 0.85

Tested by

no test coverage detected