-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from akvo/feature/16-storage-util
Feature/16 storage util
- Loading branch information
Showing
7 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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_path}/{url}") | ||
return path.is_file() | ||
|
||
def download(self, url: str): | ||
if not self.check(url): | ||
return None | ||
with open(f"{self.storage_path}/{url}", "rb") as f: | ||
return f.read() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
from django.test import TestCase | ||
from storage import Storage | ||
|
||
|
||
class StorageTestCase(TestCase): | ||
def setUp(self): | ||
# create a file | ||
self.path = "/tmp" | ||
self.file = "./test.txt" | ||
with open(self.file, "w") as f: | ||
f.write("test") | ||
self.storage = Storage(storage_path=self.path) | ||
|
||
def tearDown(self): | ||
# delete the file | ||
os.remove(self.file) | ||
|
||
def test_upload(self): | ||
self.storage.upload(file=self.file) | ||
self.assertTrue(os.path.exists(f"{self.path}/{self.file}")) | ||
|
||
# test upload with folder | ||
self.storage.upload(file=self.file, folder="testing") | ||
self.assertTrue(os.path.exists(f"{self.path}/testing/{self.file}")) | ||
# test upload with filename | ||
self.storage.upload(file=self.file, filename="test2.txt") | ||
self.assertTrue(os.path.exists(f"{self.path}/test2.txt")) | ||
|
||
def test_delete(self): | ||
self.storage.upload(file=self.file) | ||
self.storage.delete(url="test.txt") | ||
self.assertFalse(os.path.exists(f"{self.path}/{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, folder="testing") | ||
self.assertTrue(os.path.exists(f"{self.path}/testing/{self.file}")) | ||
filename = self.file.split("/")[-1] | ||
file = self.storage.download(url="testing/test.txt") | ||
with open(f"{self.path}/testing/{filename}", "rb") as f: | ||
self.assertEqual(f.read(), file) | ||
|
||
# Failed Download | ||
file = self.storage.download(url="testing/test2.txt") | ||
self.assertFalse(file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters