Skip to content

Commit

Permalink
Switch to nginx for serving files
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Nov 2, 2024
1 parent d3c635d commit 4ac0743
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 136 deletions.
140 changes: 70 additions & 70 deletions indexer/src/directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,52 +58,52 @@ async def repository_latest_request(channel, target, file_type):
except Exception as e:
return JSONResponse(str(e), status_code=404)

@router.get(prefix + "/{channel}/{file_name}")
async def repository_file_request(channel, file_name):
"""
A method for retrieving a file from a specific version
Args:
channel: Channel type (release, dev)
file_name: File Name
Returns:
Artifact file
"""
if len(index.index["channels"]) == 0:
return JSONResponse("No channels found!", status_code=404)
try:
return FileResponse(
index.get_file_path(channel, file_name),
media_type="application/octet-stream",
status_code=200,
)
except Exception as e:
return JSONResponse(str(e), status_code=404)

elif isinstance(index, PacksCatalog):

@router.get(prefix + "/{pack}/{file_type}/{file_name}")
async def pack_file_request(pack, file_type, file_name, sha256: str = None):
"""
A method for retrieving a file from a specific pack
Args:
pack: Pack id
file_type: File Type (download, preview)
file_name: File Name
Returns:
Artifact file
"""
if len(index.index["packs"]) == 0:
return JSONResponse("No packs found!", status_code=404)
try:
return FileResponse(
index.get_file_path(pack, file_type, file_name, sha256),
media_type="application/octet-stream",
status_code=200,
)
except Exception as e:
return JSONResponse(str(e), status_code=404)
# @router.get(prefix + "/{channel}/{file_name}")
# async def repository_file_request(channel, file_name):
# """
# A method for retrieving a file from a specific version
# Args:
# channel: Channel type (release, dev)
# file_name: File Name

# Returns:
# Artifact file
# """
# if len(index.index["channels"]) == 0:
# return JSONResponse("No channels found!", status_code=404)
# try:
# return FileResponse(
# index.get_file_path(channel, file_name),
# media_type="application/octet-stream",
# status_code=200,
# )
# except Exception as e:
# return JSONResponse(str(e), status_code=404)

# elif isinstance(index, PacksCatalog):

# @router.get(prefix + "/{pack}/{file_type}/{file_name}")
# async def pack_file_request(pack, file_type, file_name, sha256: str = None):
# """
# A method for retrieving a file from a specific pack
# Args:
# pack: Pack id
# file_type: File Type (download, preview)
# file_name: File Name

# Returns:
# Artifact file
# """
# if len(index.index["packs"]) == 0:
# return JSONResponse("No packs found!", status_code=404)
# try:
# return FileResponse(
# index.get_file_path(pack, file_type, file_name, sha256),
# media_type="application/octet-stream",
# status_code=200,
# )
# except Exception as e:
# return JSONResponse(str(e), status_code=404)

@router.get(prefix + "/reindex")
async def reindex_request():
Expand All @@ -123,30 +123,30 @@ async def reindex_request():
logging.exception(e)
return JSONResponse("Reindexing is failed!", status_code=500)

if isinstance(index, RepositoryIndex):

@router.get(prefix + "/{branch}")
async def repository_branch_request(branch):
"""
A method for retrieving the list of files from a specific branch
Made for support of `ufbt update --index-url {base_url}/firmware --branch {branch}`
Args:
branch: Branch name
Returns:
HTML links in format that ufbt understands
"""
if len(index.index["channels"]) == 0:
return JSONResponse("No channels found!", status_code=404)
try:
branch_files = index.get_branch_file_names(branch)
response = "\n".join(f'<a href="{file}"></a>' for file in branch_files)
return HTMLResponse(
response,
status_code=200,
)
except Exception as e:
return JSONResponse(str(e), status_code=404)
# if isinstance(index, RepositoryIndex):

# @router.get(prefix + "/{branch}")
# async def repository_branch_request(branch):
# """
# A method for retrieving the list of files from a specific branch
# Made for support of `ufbt update --index-url {base_url}/firmware --branch {branch}`
# Args:
# branch: Branch name

# Returns:
# HTML links in format that ufbt understands
# """
# if len(index.index["channels"]) == 0:
# return JSONResponse("No channels found!", status_code=404)
# try:
# branch_files = index.get_branch_file_names(branch)
# response = "\n".join(f'<a href="{file}"></a>' for file in branch_files)
# return HTMLResponse(
# response,
# status_code=200,
# )
# except Exception as e:
# return JSONResponse(str(e), status_code=404)


for directory, index in indexes.items():
Expand Down
114 changes: 57 additions & 57 deletions indexer/src/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ def reindex(self):
logging.exception(e)
raise e

def get_branch_file_names(self: str, branch: str) -> list[str]:
"""
A method to get a list of file names in the specified branch
Args:
branch: Branch name
# def get_branch_file_names(self: str, branch: str) -> list[str]:
# """
# A method to get a list of file names in the specified branch
# Args:
# branch: Branch name

Returns:
The list of file names
"""
branch_path = os.path.join(settings.files_dir, self.directory, branch)
if not os.path.isdir(branch_path):
raise FileNotFoundError("Branch not found!")
files = os.listdir(branch_path)
return filter(lambda file: not file.startswith("."), files)
# Returns:
# The list of file names
# """
# branch_path = os.path.join(settings.files_dir, self.directory, branch)
# if not os.path.isdir(branch_path):
# raise FileNotFoundError("Branch not found!")
# files = os.listdir(branch_path)
# return filter(lambda file: not file.startswith("."), files)

def get_file_from_latest_version(
self: str, channel: str, target: str, file_type: str
Expand Down Expand Up @@ -148,20 +148,20 @@ def get_file_from_latest_version(
logging.exception(e)
raise e

def get_file_path(self: str, channel: str, file_name: str) -> str:
"""
A method to get a specific file by name in the specified channel
Args:
channel: Channel type (release, dev)
file_name: File Name
# def get_file_path(self: str, channel: str, file_name: str) -> str:
# """
# A method to get a specific file by name in the specified channel
# Args:
# channel: Channel type (release, dev)
# file_name: File Name

Returns:
The file path
"""
file_path = os.path.join(settings.files_dir, self.directory, channel, file_name)
if not os.path.isfile(file_path):
raise FileNotFoundError("File not found, try a newer link!")
return file_path
# Returns:
# The file path
# """
# file_path = os.path.join(settings.files_dir, self.directory, channel, file_name)
# if not os.path.isfile(file_path):
# raise FileNotFoundError("File not found, try a newer link!")
# return file_path


class PacksCatalog:
Expand Down Expand Up @@ -212,38 +212,38 @@ def reindex(self):
logging.exception(e)
raise e

def get_file_path(
self: str, pack: str, file_type: str, file_name: str, sha256: str
) -> str:
"""
A method to get a specific file by type and name in the specified pack
Args:
pack: Pack id
file_type: File Type (download, preview)
file_name: File Name
# def get_file_path(
# self: str, pack: str, file_type: str, file_name: str, sha256: str
# ) -> str:
# """
# A method to get a specific file by type and name in the specified pack
# Args:
# pack: Pack id
# file_type: File Type (download, preview)
# file_name: File Name

Returns:
The file path
"""
file_path = os.path.join(
settings.files_dir, self.directory, pack, file_type, file_name
)
if file_type not in ("download", "preview") or not os.path.isfile(file_path):
raise FileNotFoundError("File not found, try a newer link!")
if sha256 and file_type == "download":
matches = False
for pack_i in self.index["packs"]:
if pack_i["id"] != pack:
continue
for file_i in pack_i["files"]:
if not file_i["url"].endswith(file_name):
continue
matches = file_i["sha256"] == sha256
break
break
if not matches:
raise FileNotFoundError("File not found, try a newer link!")
return file_path
# Returns:
# The file path
# """
# file_path = os.path.join(
# settings.files_dir, self.directory, pack, file_type, file_name
# )
# if file_type not in ("download", "preview") or not os.path.isfile(file_path):
# raise FileNotFoundError("File not found, try a newer link!")
# if sha256 and file_type == "download":
# matches = False
# for pack_i in self.index["packs"]:
# if pack_i["id"] != pack:
# continue
# for file_i in pack_i["files"]:
# if not file_i["url"].endswith(file_name):
# continue
# matches = file_i["sha256"] == sha256
# break
# break
# if not matches:
# raise FileNotFoundError("File not found, try a newer link!")
# return file_path


indexes = {
Expand Down
2 changes: 1 addition & 1 deletion indexer/src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Settings(BaseModel):
port=8000,
workers=1,
files_dir=str(pathlib.Path(__file__).parent.parent.parent / "files"),
base_url="https://up.momentum-fw.dev/",
base_url="https://up.momentum-fw.dev/builds",
token=os.getenv("INDEXER_TOKEN"),
github_org="Next-Flip",
gelf_host=os.getenv("GELF_HOST"),
Expand Down
5 changes: 2 additions & 3 deletions nginx/nginx-theme/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<title>Flipper Zero Firmware Update</title>
<title>Momentum FW Update Server</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
Expand All @@ -14,15 +14,14 @@

<script type="text/javascript" src="/builds/nginx-theme/js/breadcrumbs.js"></script>
<script type="text/javascript" src="/builds/nginx-theme/js/list.js"></script>
<script defer data-domain="update.flipperzero.one" src="https://analytics.flipperdevices.com/js/plausible.js"></script>
</head>

<body>
<div class="header">
<nav class="navbar sticky-top container">
<div class="navbar-brand">
<i class="fa fa-fw fa-files-o" aria-hidden="true"></i>
Flipper Zero Update Server
Momentum FW Update Server
</div>
</nav>
</div>
Expand Down
2 changes: 1 addition & 1 deletion nginx/nginx-theme/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* 2018 Alexander Haase <ahaase@alexhaase.de>
*
* See the LICENSE file for details.
*/body{background-color:#f8f9fa;padding-bottom:20px}@media (prefers-color-scheme:white){body{background-color:#343a40;color:#fff}}.breadcrumb{background-color:transparent;padding-left:35px}.breadcrumb .breadcrumb-item a{color:#4caf50}html{position:relative;min-height:100%}.footer{position:absolute;bottom:0;width:100%;margin-bottom:0}.footer a,.footer a:focus,.footer a:hover{color:#4caf50}.header{background-color:#4caf50;color:#fff;min-height:24px}.header .navbar-brand{padding:0 8px;font-size:16px;line-height:24px;height:24px}#list a,#list a:focus,#list a:hover{color:#000}#list colgroup{display:none}#list .filename{word-break:break-all;white-space:normal}@media (prefers-color-scheme:dark){a,a:focus,a:hover{color:#000}}
*/body{background-color:#f8f9fa;padding-bottom:20px}@media (prefers-color-scheme:white){body{background-color:#343a40;color:#fff}}.breadcrumb{background-color:transparent;padding-left:35px}.breadcrumb .breadcrumb-item a{color:#a883e9}html{position:relative;min-height:100%}.footer{position:absolute;bottom:0;width:100%;margin-bottom:0}.footer a,.footer a:focus,.footer a:hover{color:#a883e9}.header{background-color:#a883e9;color:#fff;min-height:24px}.header .navbar-brand{padding:0 8px;font-size:16px;line-height:24px;height:24px}#list a,#list a:focus,#list a:hover{color:#000}#list colgroup{display:none}#list .filename{word-break:break-all;white-space:normal}@media (prefers-color-scheme:dark){a,a:focus,a:hover{color:#000}}
8 changes: 4 additions & 4 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ http {
location /builds/nginx-theme/ {
more_set_headers -s '200 201 204 206 301 302 303 304 307 308' 'Cache-Control: public, max-age=1209600, s-max-age=1209600';
more_set_headers -s '400 404 413 500 503' 'Cache-Control: no-cache, max-age=0, s-max-age=0, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0';
alias /var/lib/nginx/html/nginx-theme/;
alias /opt/indexer/nginx/nginx-theme/;
}
location /builds {
root /files;
alias /opt/indexer/files/;
more_set_headers -s '200 201 204 206 301 302 303 304 307 308' 'Cache-Control: public, max-age=1209600, s-max-age=1209600';
more_set_headers -s '400 404 413 500 503' 'Cache-Control: no-cache, max-age=0, s-max-age=0, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0';
fancyindex on;
fancyindex_header "/nginx-theme/header.html";
fancyindex_footer "/nginx-theme/footer.html";
fancyindex_header "/builds/nginx-theme/header.html";
fancyindex_footer "/builds/nginx-theme/footer.html";
fancyindex_default_sort date_desc;
fancyindex_show_path off;
fancyindex_name_length 255;
Expand Down

0 comments on commit 4ac0743

Please sign in to comment.