Skip to content

Commit

Permalink
Merge pull request #54 from openstreetmap-polska/dev
Browse files Browse the repository at this point in the history
Release to main
  • Loading branch information
Zaczero authored Feb 13, 2024
2 parents 2d64899 + 69b6d41 commit c6eb2ac
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
5 changes: 5 additions & 0 deletions api/v1/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from models.lonlat import LonLat
from states.aed_state import AEDStateDep
from states.photo_state import PhotoStateDep
from utils import get_wikimedia_commons_url

router = APIRouter()

Expand Down Expand Up @@ -43,12 +44,14 @@ async def _get_image_data(tags: dict[str, str], photo_state: PhotoStateDep) -> d
return {
'@photo_id': photo_info.id,
'@photo_url': f'/api/v1/photos/view/{photo_info.id}.webp',
'@photo_source': None,
}

if image_url:
return {
'@photo_id': None,
'@photo_url': f'/api/v1/photos/proxy/direct/{quote_plus(image_url)}',
'@photo_source': image_url,
}

wikimedia_commons: str = tags.get('wikimedia_commons', '')
Expand All @@ -57,11 +60,13 @@ async def _get_image_data(tags: dict[str, str], photo_state: PhotoStateDep) -> d
return {
'@photo_id': None,
'@photo_url': f'/api/v1/photos/proxy/wikimedia-commons/{quote_plus(wikimedia_commons)}',
'@photo_source': get_wikimedia_commons_url(wikimedia_commons),
}

return {
'@photo_id': None,
'@photo_url': None,
'@photo_source': None,
}


Expand Down
20 changes: 4 additions & 16 deletions api/v1/photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from states.aed_state import AEDStateDep
from states.photo_report_state import PhotoReportStateDep
from states.photo_state import PhotoStateDep
from utils import get_http_client
from utils import get_http_client, get_wikimedia_commons_url

router = APIRouter(prefix='/photos')

Expand Down Expand Up @@ -65,19 +65,13 @@ async def view(id: str, photo_state: PhotoStateDep) -> FileResponse:
async def proxy_direct(url_encoded: str) -> FileResponse:
url = unquote_plus(url_encoded)
file, content_type = await _fetch_image(url)
return Response(
file,
media_type=content_type,
headers={
'X-Image-Source': url,
},
)
return Response(file, media_type=content_type)


@router.get('/proxy/wikimedia-commons/{path_encoded:path}')
@configure_cache(timedelta(days=7), stale=timedelta(days=7))
async def proxy_wikimedia_commons(path_encoded: str) -> FileResponse:
meta_url = f'https://commons.wikimedia.org/wiki/{unquote_plus(path_encoded)}'
meta_url = get_wikimedia_commons_url(unquote_plus(path_encoded))

async with get_http_client() as http:
r = await http.get(meta_url)
Expand All @@ -90,13 +84,7 @@ async def proxy_wikimedia_commons(path_encoded: str) -> FileResponse:

image_url = og_image['content']
file, content_type = await _fetch_image(image_url)
return Response(
file,
media_type=content_type,
headers={
'X-Image-Source': meta_url,
},
)
return Response(file, media_type=content_type)


@router.post('/upload')
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pyproj import Transformer

NAME = 'openaedmap-backend'
VERSION = '2.6.0'
VERSION = '2.6.1'
VERSION_TIMESTAMP = 0
CREATED_BY = f'{NAME} {VERSION}'
WEBSITE = 'https://openaedmap.org'
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
let
# Currently using nixpkgs-23.11-darwin
# Update with `nixpkgs-update` command
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/5cd1bc5c4d702f3ccc26fa93f86d05d5684fe5a3.tar.gz") { };
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/6716c0e608eed726549fd92704b9a7a7077bdf00.tar.gz") { };

libraries' = with pkgs; [
# Base libraries
Expand Down
4 changes: 4 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ def as_dict(data) -> dict:
d[k] = mapping(v)

return d


def get_wikimedia_commons_url(path: str) -> str:
return f'https://commons.wikimedia.org/wiki/{path}'

0 comments on commit c6eb2ac

Please sign in to comment.