Skip to content

Commit a80a98e

Browse files
committed
Add uploading files without reindex, minor fixes
1 parent 0fef0da commit a80a98e

File tree

8 files changed

+69
-11
lines changed

8 files changed

+69
-11
lines changed

Dockerfile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
FROM python:3.11-alpine3.17
22

3+
RUN apk update
4+
RUN apk add tzdata nginx-mod-http-fancyindex nginx-mod-http-headers-more bash
5+
6+
ADD requirements.txt /app/
7+
RUN python3 -m pip install -r /app/requirements.txt
8+
39
COPY nginx/nginx.conf /etc/nginx/nginx.conf
410
COPY nginx/nginx-theme /var/lib/nginx/html/nginx-theme
511
ADD indexer /app
612
COPY startup.sh /app/
7-
WORKDIR /app
813

9-
RUN apk update
10-
RUN apk add tzdata nginx-mod-http-fancyindex nginx-mod-http-headers-more bash
11-
RUN python3 -m pip install -r requirements.txt
14+
WORKDIR /app
15+
CMD ["/bin/bash", "/app/startup.sh"]
1216

13-
CMD ["/bin/bash", "startup.sh"]

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ Upload files
4747
-F "files=@flipper-z-f7-full-0.73.1.json" \
4848
127.0.0.1:8000/firmware/uploadfiles
4949
```
50+
51+
Upload files without reindex
52+
```bash
53+
curl -L -H "Token: YOUR_TOKEN" \
54+
-F "files=@gcc-arm-none-eabi-12.3-arm64-darwin-flipper-24.tar.gz" \
55+
127.0.0.1:8000/toolchain/uploadfilesraw
56+
```

indexer/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import uvicorn
66
from fastapi import FastAPI, Request, Response
77
from src import directories, file_upload, security
8-
from src.directories import indexes
8+
from src.repository import indexes, raw_file_upload_directories
99
from src.settings import settings
1010
from pygelf import GelfTcpHandler
1111

@@ -25,9 +25,17 @@ def startup_event() -> None:
2525
os.makedirs(settings.files_dir)
2626
for index in indexes:
2727
try:
28+
index_path = os.path.join(settings.files_dir, index)
29+
os.makedirs(index_path, exist_ok=True)
2830
indexes[index].reindex()
2931
except Exception:
3032
logging.exception(f"Init {index} reindex failed")
33+
for raw_upload_dir in raw_file_upload_directories:
34+
try:
35+
dir_path = os.path.join(settings.files_dir, raw_upload_dir)
36+
os.makedirs(dir_path, exist_ok=True)
37+
except Exception:
38+
logging.exception(f"Failed to create {dir_path}")
3139

3240

3341
app.include_router(file_upload.router)

indexer/src/file_upload.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import List
88
from fastapi import APIRouter, Form, UploadFile
99
from fastapi.responses import JSONResponse
10-
from .directories import indexes
10+
from .repository import indexes, raw_file_upload_directories
1111
from .settings import settings
1212

1313

@@ -46,7 +46,7 @@ def save_files(path: str, files: List[UploadFile]) -> None:
4646
out_file.write(file.file.read())
4747

4848

49-
def move_files(dest_dir: str, source_dir: str, version_token: str) -> None:
49+
def move_files_for_indexed(dest_dir: str, source_dir: str, version_token: str) -> None:
5050
token_file_path = os.path.join(dest_dir, TOKEN_FILENAME)
5151
do_cleanup = False
5252
if version_token and os.path.isfile(token_file_path):
@@ -67,6 +67,13 @@ def move_files(dest_dir: str, source_dir: str, version_token: str) -> None:
6767
shutil.move(sourcefilepath, destfilepath)
6868

6969

70+
def move_files_raw(dest_dir: str, source_dir: str) -> None:
71+
for file in os.listdir(source_dir):
72+
sourcefilepath = os.path.join(source_dir, file)
73+
destfilepath = os.path.join(dest_dir, file)
74+
shutil.move(sourcefilepath, destfilepath)
75+
76+
7077
@router.post("/{directory}/uploadfiles")
7178
async def create_upload_files(
7279
directory: str,
@@ -101,7 +108,7 @@ async def create_upload_files(
101108
try:
102109
with tempfile.TemporaryDirectory() as temp_path:
103110
save_files(temp_path, files)
104-
move_files(final_path, temp_path, version_token)
111+
move_files_for_indexed(final_path, temp_path, version_token)
105112
logging.info(f"Uploaded {len(files)} files")
106113
except Exception as e:
107114
logging.exception(e)
@@ -117,3 +124,34 @@ async def create_upload_files(
117124
)
118125
else:
119126
return JSONResponse("File uploaded, reindexing isn't needed!")
127+
128+
129+
@router.post("/{directory}/uploadfilesraw")
130+
async def create_upload_files_raw(
131+
directory: str,
132+
files: List[UploadFile],
133+
):
134+
"""
135+
A method to upload files in a certain directory without indexing
136+
Args:
137+
directory: Repository name
138+
files: File list
139+
140+
Returns:
141+
Upload status
142+
"""
143+
if directory not in raw_file_upload_directories:
144+
return JSONResponse(f"{directory} not found!", status_code=404)
145+
146+
project_root_path = os.path.join(settings.files_dir, directory)
147+
148+
async with lock:
149+
try:
150+
with tempfile.TemporaryDirectory() as temp_path:
151+
save_files(temp_path, files)
152+
move_files_raw(project_root_path, temp_path)
153+
logging.info(f"Uploaded {len(files)} files")
154+
return JSONResponse("File uploaded")
155+
except Exception as e:
156+
logging.exception(e)
157+
return JSONResponse(str(e), status_code=500)

indexer/src/repository.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,5 @@ def get_file_from_latest_version(
155155
file_parser=blackmagicFileParser,
156156
),
157157
}
158+
159+
raw_file_upload_directories = ["toolchain"]

indexer/src/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ class Settings(BaseModel):
4444
qFlipper_github_repo=os.getenv("INDEXER_QFLIPPER_GITHUB_REPO"),
4545
blackmagic_github_token=os.getenv("INDEXER_BLACKMAGIC_GITHUB_TOKEN"),
4646
blackmagic_github_repo=os.getenv("INDEXER_BLACKMAGIC_GITHUB_REPO"),
47-
private_paths=["reindex", "uploadfiles"],
47+
private_paths=["reindex", "uploadfiles", "uploadfilesraw"],
4848
)

nginx/nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ http {
3737
fancyindex_localtime on;
3838
fancyindex_ignore "nginx-theme";
3939
}
40-
location ~ ^/(qFlipper|firmware|blackmagic-firmware)/ {
40+
location ~ ^/(qFlipper|firmware|blackmagic-firmware|toolchain)/ {
4141
more_set_headers 'Cache-Control: no-cache, max-age=0, s-max-age=0, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0';
4242
proxy_set_header Host $host;
4343
proxy_set_header X-Real-IP $remote_addr;
File renamed without changes.

0 commit comments

Comments
 (0)