Skip to content

Commit

Permalink
[#16] Init storage service util
Browse files Browse the repository at this point in the history
  • Loading branch information
dedenbangkit committed Sep 7, 2023
1 parent 1371df0 commit b071de8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
30 changes: 30 additions & 0 deletions backend/akvo/utils/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
from pathlib import Path
import shutil


class Storage:
def __init__(self, storage_path: str):
self.storage_path = storage_path

def upload(self, file: str, folder: str = None, filename: str = None):
storage_location = self.storage_path
if folder:
Path(f"{storage_location}/{folder}").mkdir(parents=True, exist_ok=True)
storage_location = f"{storage_location}/{folder}"
if not filename:
filename = file.split("/")[-1]
location = f"{storage_location}/{filename}"
shutil.copy2(file, location)
return location

def delete(self, url: str):
os.remove(f"{self.storage_path}/{url}")
return url

def check(self, url: str):
path = Path(f"{self.storage_location}/{url}")
return path.is_file()

def download(self, url: str):
return f"{self.storage_path}/{url}"
Empty file.
32 changes: 32 additions & 0 deletions backend/akvo/utils/tests/test_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
from django.test import TestCase
from storage import Storage


class StorageTestCase(TestCase):
def setUp(self):
# Create a temporary directory if it doesn't exist
if not os.path.exists("/tmp"):
os.mkdir("/tmp")
# create a file
self.file = "./test.txt"
with open(self.file, "w") as f:
f.write("test")
self.storage = Storage(storage_path="/tmp")

def test_upload(self):
self.storage.upload(file=self.file)
self.assertTrue(os.path.exists(self.file))

def test_delete(self):
self.storage.upload(file=self.file)
self.storage.delete(url="test.txt")
self.assertFalse(os.path.exists(self.file))

def test_check(self):
self.storage.upload(file=self.file)
self.assertTrue(self.storage.check(url="test.txt"))

def test_download(self):
self.storage.upload(file=self.file)
self.assertEqual(self.storage.download(url="test.txt"), self.file)
4 changes: 4 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ services:
- 8888:8888
frontend:
network_mode: service:mainnetwork
volumes:
- ${STORAGE_PATH}/images:/app/public/images:delegated
backend:
environment:
- WEBDOMAIN=https://afs.akvotest.org
network_mode: service:mainnetwork
volumes:
- ${STORAGE_PATH}:/app/storage:delegated
pgadmin:
image: dpage/pgadmin4:5.7
environment:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
- DB_HOST=db
- DEBUG=True
- DJANGO_SECRET=local-secret
- STORAGE_PATH
depends_on:
- db
frontend:
Expand Down

0 comments on commit b071de8

Please sign in to comment.