Skip to content

Commit fa81f21

Browse files
committed
Save image file to server
1 parent 8a4de82 commit fa81f21

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

go_capture/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,6 @@
123123
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
124124

125125
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
126+
127+
IMAGES_DIR = BASE_DIR / 'images'
128+
IMAGES_DIR.mkdir(exist_ok=True)

go_capture/views.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import io
22
from pathlib import Path
33

4-
from django.http import FileResponse, JsonResponse
4+
from django.conf import settings
5+
from django.http import JsonResponse, HttpResponse
56
from django.views.decorators.http import require_POST, require_GET
67

78
from go_capture.sgf.process_image import process_image
@@ -10,17 +11,11 @@
1011
@require_POST
1112
def capture(request):
1213
image_file = request.FILES['image']
13-
output_file = io.StringIO()
14-
process_image(image_file, output_file)
15-
output_file.seek(0)
16-
filename = Path(image_file.name).stem
17-
return FileResponse(
18-
output_file.read(),
19-
status=201,
20-
filename=f'${filename}.sgf',
21-
as_attachment=True,
22-
content_type='application/x-go-sgf'
23-
)
14+
filename = Path(image_file.name)
15+
output_path = settings.IMAGES_DIR / filename
16+
with output_path.open('wb') as output_file:
17+
output_file.write(image_file.read())
18+
return HttpResponse(status=201)
2419

2520

2621
@require_GET

0 commit comments

Comments
 (0)