Skip to content

Commit

Permalink
refactor: use white image as a dummy avatar
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Dec 11, 2024
1 parent 6b4b69b commit 7a7bd0c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 31 deletions.
Binary file added plugin/assets/white-pixel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions plugin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path
from typing import Any, Callable, Literal, Sequence, cast

import requests
import sublime
from LSP.plugin.core.protocol import Position as LspPosition
from LSP.plugin.core.protocol import Range as LspRange
Expand Down Expand Up @@ -39,7 +40,6 @@
get_project_relative_path,
get_view_language_id,
set_copilot_setting,
simple_urlopen,
)


Expand Down Expand Up @@ -95,10 +95,13 @@ def update_avatar(cls, username: str, *, size: int = 64) -> None:
cls.clear_avatar()
return

try:
data = simple_urlopen(f"https://github.com/{username}.png?size={size}")
except Exception as e:
log_error(f'Failed to fetch avatar for "{username}" because: {e}')
if (req := requests.get(f"https://github.com/{username}.png?size={size}")).ok:
data = req.content
# see https://github.com/TerminalFi/LSP-copilot/issues/218#issuecomment-2535522265
elif req.status_code == 404:
data = sublime.load_binary_resource(f"Packages/{PACKAGE_NAME}/plugin/assets/white-pixel.png")
else:
log_error(f'Failed to fetch avatar for "{username}" with status code {req.status_code}.')
cls.clear_avatar()
return

Expand Down
26 changes: 0 additions & 26 deletions plugin/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from __future__ import annotations

import contextlib
import gzip
import os
import sys
import threading
import urllib.request
from collections.abc import Callable, Generator, Iterable
from functools import wraps
from typing import Any, Mapping, Sequence, TypeVar, Union, cast
Expand Down Expand Up @@ -236,27 +234,3 @@ def find_index_by_key_value(items: Sequence[Mapping[_KT, _VT]], key: _KT, value:
If not found, returns `-1`.
"""
return first((idx for idx, item in enumerate(items) if key in item and item[key] == value), -1)


def simple_urlopen(url: str, *, chunk_size: int = 512 * 1024) -> bytes:
"""
Opens a URL and reads the data in chunks, optionally decompressing gzip-encoded content.
This function opens a connection to the specified URL and reads the response data in chunks
of a specified size. If the response is gzip-encoded, it decompresses the data before returning it.
Args:
url (str): The URL to open.
chunk_size (int, optional): The size of each chunk to read. Defaults to 512KB.
Returns:
bytes: The raw bytes of the data read from the URL. If the content was gzip-encoded,
it is decompressed before being returned.
"""
with urllib.request.urlopen(url) as resp:
data = b""
while chunk := resp.read(chunk_size):
data += chunk
if resp.info().get("Content-Encoding") == "gzip":
data = gzip.decompress(data)
return data

0 comments on commit 7a7bd0c

Please sign in to comment.