Skip to content

Commit

Permalink
Merge 1bd0c02 into 0207b42
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Oct 5, 2018
2 parents 0207b42 + 1bd0c02 commit fe03ba2
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
71 changes: 71 additions & 0 deletions masonite/drivers/UploadAzureDriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
""" Azure Upload Driver """

from masonite.contracts import UploadContract
from masonite.drivers import BaseUploadDriver
from masonite.exceptions import DriverLibraryNotFound
from masonite.helpers import random_string
from masonite.managers import UploadManager

from config import storage


class UploadAzureDriver(BaseUploadDriver, UploadContract):

def __init__(self, upload: UploadManager):
"""Upload Azure Driver Constructor
Arguments:
UploadManager {masonite.managers.UploadManager} -- The Upload Manager object.
"""

self.upload = upload
self.config = storage

def store(self, fileitem, location=None):
"""Store the file into Microsoft Azure server.
Arguments:
fileitem {cgi.Storage} -- Storage object.
Keyword Arguments:
location {string} -- The location on disk you would like to store the file. (default: {None})
Raises:
DriverLibraryNotFound -- Raises when the azure library is not installed.
Returns:
string -- Returns the file name just saved.
"""

try:
from azure.storage.blob import BlockBlobService
except ImportError:
raise DriverLibraryNotFound("Could not find the 'azure' driver")

block_blob_service = BlockBlobService(
account_name=self.config.DRIVERS['azure']['name'], account_key=self.config.DRIVERS['azure']['secret'])

# Store temporarily on disk
driver = self.upload.driver('disk')
driver.store(fileitem, location)
file_location = driver.file_location

filename = random_string(25) + fileitem.filename

block_blob_service.create_blob_from_path(
self.config.DRIVERS['azure']['container'], filename, file_location)

return filename

def store_prepend(self, fileitem, prepend, location=None):
"""Store the file onto the Rackspace server but with a prepended file name.
Arguments:
fileitem {cgi.Storage} -- Storage object.
prepend {string} -- The prefix you want to prepend to the file name.
Keyword Arguments:
location {string} -- The location on disk you would like to store the file. (default: {None})
Returns:
string -- Returns the file name just saved.
"""

fileitem.filename = prepend + fileitem.filename

return self.store(fileitem, location=location)
70 changes: 70 additions & 0 deletions masonite/drivers/UploadRackspaceDriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
""" Upload Rackspace Driver """

from masonite.contracts import UploadContract
from masonite.drivers import BaseUploadDriver
from masonite.exceptions import DriverLibraryNotFound
from masonite.helpers import random_string
from masonite.managers import UploadManager

from config import storage


class UploadRackspaceDriver(BaseUploadDriver, UploadContract):

def __init__(self, upload: UploadManager):
"""Upload Rackspace Driver Constructor
Arguments:
UploadManager {masonite.managers.UploadManager} -- The Upload Manager object.
"""

self.upload = upload
self.config = storage

def store(self, fileitem, location=None):
"""Store the file into Rackspace server.
Arguments:
fileitem {cgi.Storage} -- Storage object.
Keyword Arguments:
location {string} -- The location on disk you would like to store the file. (default: {None})
Raises:
DriverLibraryNotFound -- Raises when the rackspace library is not installed.
Returns:
string -- Returns the file name just saved.
"""

try:
from rackspace import connection
except ImportError:
raise DriverLibraryNotFound(
"Could not find the required 'rackspace' driver")

conn = connection.Connection(username=self.config.DRIVERS['rackspace']['username'],
api_key=self.config.DRIVERS['rackspace']['secret'],
region=self.config.DRIVERS['rackspace']['region'])

filename = random_string(25) + fileitem.filename

self.validate_extension(filename)

conn.object_store.upload_object(container=self.config.DRIVERS['rackspace']['container'],
name=filename,
data=fileitem.file.read())
return filename

def store_prepend(self, fileitem, prepend, location=None):
"""Store the file onto the Rackspace server but with a prepended file name.
Arguments:
fileitem {cgi.Storage} -- Storage object.
prepend {string} -- The prefix you want to prepend to the file name.
Keyword Arguments:
location {string} -- The location on disk you would like to store the file. (default: {None})
Returns:
string -- Returns the file name just saved.
"""

fileitem.filename = prepend + fileitem.filename

return self.store(fileitem, location=location)

0 comments on commit fe03ba2

Please sign in to comment.