Skip to content

Commit

Permalink
Merge pull request #476 from MasoniteFramework/improve-upload-driver
Browse files Browse the repository at this point in the history
can now upload files directly to s3 using open()
  • Loading branch information
josephmancuso committed Nov 10, 2018
2 parents 498eb27 + 6cb97eb commit 04b63d5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
8 changes: 8 additions & 0 deletions masonite/drivers/BaseUploadDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from masonite.exceptions import FileTypeException
from masonite.drivers.BaseDriver import BaseDriver
import _io


class BaseUploadDriver(BaseDriver):
Expand Down Expand Up @@ -57,3 +58,10 @@ def get_location(self, location=None):
return list(location.values())[0]

return location

def get_name(self, fileitem):
if isinstance(fileitem, _io.TextIOWrapper):
# It is an open() file
return fileitem.name
else:
return fileitem.filename
8 changes: 6 additions & 2 deletions masonite/drivers/UploadDiskDriver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Upload Disk Driver """

import os
import _io

from masonite.contracts.UploadContract import UploadContract
from masonite.drivers.BaseUploadDriver import BaseUploadDriver
Expand Down Expand Up @@ -36,7 +37,7 @@ def store(self, fileitem, location=None):
string -- Returns the file name just saved.
"""

filename = os.path.basename(fileitem.filename)
filename = self.get_name(fileitem)

# Check if is a valid extension
self.validate_extension(filename)
Expand All @@ -45,7 +46,10 @@ def store(self, fileitem, location=None):
if not location.endswith('/'):
location += '/'

open(location + filename, 'wb').write(fileitem.file.read())
if isinstance(fileitem, _io.TextIOWrapper):
open(location + filename, 'wb').write(bytes(fileitem.read(), 'utf-8'))
else:
open(location + filename, 'wb').write(fileitem.file.read())

self.file_location = location + filename

Expand Down
8 changes: 5 additions & 3 deletions masonite/drivers/UploadS3Driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ def store(self, fileitem, location=None):
driver.store(fileitem, location)
file_location = driver.file_location

filename = self.get_name(fileitem)

# Check if is a valid extension
self.validate_extension(fileitem.filename)
self.validate_extension(filename)

try:
import boto3
Expand All @@ -60,10 +62,10 @@ def store(self, fileitem, location=None):
s3.meta.client.upload_file(
file_location,
self.config.DRIVERS['s3']['bucket'],
fileitem.filename
filename
)

return fileitem.filename
return filename

def store_prepend(self, fileitem, prepend, location=None):
"""Store the file onto the Amazon S3 server but with a prepended file name.
Expand Down
2 changes: 1 addition & 1 deletion masonite/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"""


VERSION = '2.0.34'
VERSION = '2.0.35'
3 changes: 3 additions & 0 deletions tests/test_upload_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def setup_method(self):
def test_upload_file_for_s3(self):
assert self.app.make('Upload').driver('s3').store(ImageMock()) == 'test.jpg'

def test_upload_open_file_for_s3(self):
assert self.app.make('Upload').driver('s3').store(open('.travis.yml'))


def test_upload_manage_accept_files(self):
"""
Expand Down

0 comments on commit 04b63d5

Please sign in to comment.