mirror of
https://github.com/sissbruecker/linkding.git
synced 2025-08-08 03:08:29 +02:00
49 lines
1.0 KiB
Python
49 lines
1.0 KiB
Python
import gzip
|
|
import os
|
|
|
|
from django.conf import settings
|
|
from django.http import (
|
|
HttpResponse,
|
|
Http404,
|
|
)
|
|
from django.shortcuts import render
|
|
|
|
from bookmarks.views import access
|
|
|
|
|
|
def _get_asset_content(asset):
|
|
filepath = os.path.join(settings.LD_ASSET_FOLDER, asset.file)
|
|
|
|
if not os.path.exists(filepath):
|
|
raise Http404("Asset file does not exist")
|
|
|
|
if asset.gzip:
|
|
with gzip.open(filepath, "rb") as f:
|
|
content = f.read()
|
|
else:
|
|
with open(filepath, "rb") as f:
|
|
content = f.read()
|
|
|
|
return content
|
|
|
|
|
|
def view(request, asset_id: int):
|
|
asset = access.asset_read(request, asset_id)
|
|
content = _get_asset_content(asset)
|
|
|
|
return HttpResponse(content, content_type=asset.content_type)
|
|
|
|
|
|
def read(request, asset_id: int):
|
|
asset = access.asset_read(request, asset_id)
|
|
content = _get_asset_content(asset)
|
|
content = content.decode("utf-8")
|
|
|
|
return render(
|
|
request,
|
|
"bookmarks/read.html",
|
|
{
|
|
"content": content,
|
|
},
|
|
)
|