# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2014 SF Isle of Man Limited
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa.  If not, see <http://www.gnu.org/licenses/>.
# Cache global variables for timeouts
"""
Local module for uploading files to a PyBossa local filesystem.

This module exports:
    * Local class: for uploading files to a local filesystem.

"""
import pyrax
from pybossa.uploader import Uploader
from werkzeug import secure_filename


class RackspaceUploader(Uploader):

    """Rackspace Cloud Files uploader class."""

    cf = None
    cont_name = "pybossa"
    container = None

    def __init__(self, username, api_key, region, cont_name=None, **kwargs):
        """Init method to create a generic uploader."""
        try:
            pyrax.set_setting("identity_type", "rackspace")
            pyrax.set_credentials(username, api_key, region=region)
            self.cf = pyrax.cloudfiles
            if cont_name:
                self.cont_name = cont_name
            self.container = self.cf.get_container(self.cont_name)
            super(self.__class__, self).__init__(**kwargs)
        except pyrax.exceptions.NoSuchContainer:
            self.cf.create_container(self.cont_name)
            self.container = self.cf.get_container(self.cont_name)

    def upload_file(self, file):
        """Upload a file into a container."""
        try:
            chksum = pyrax.utils.get_checksum(file)
            self.cf.upload_file(self.cont_name,
                                file,
                                obj_name=secure_filename(file.filename),
                                etag=chksum)
            return True
        except pyrax.exceptions.UploadFailed:
            return False

# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2014 SF Isle of Man Limited
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa.  If not, see <http://www.gnu.org/licenses/>.
# Cache global variables for timeouts
"""
Local module for uploading files to a PyBossa local filesystem.

This module exports:
    * Local class: for uploading files to a local filesystem.

"""
import pyrax
from pybossa.uploader import Uploader
from werkzeug import secure_filename


class RackspaceUploader(Uploader):

    """Rackspace Cloud Files uploader class."""

    cf = None
    cont_name = "pybossa"

    def __init__(self, username, api_key, region, cont_name=None, **kwargs):
        """Init method to create a generic uploader."""
        try:
            pyrax.set_setting("identity_type", "rackspace")
            pyrax.set_credentials(username, api_key, region=region)
            self.cf = pyrax.cloudfiles
            if cont_name:
                self.cont_name = cont_name
            self.cf.get_container(self.cont_name)
            super(self.__class__, self).__init__(**kwargs)
        except pyrax.exceptions.NoSuchContainer:
            self.cf.create_container(self.cont_name)
            self.cf.make_container_public(self.cont_name)

    def upload_file(self, file):
        """Upload a file into a container."""
        try:
            chksum = pyrax.utils.get_checksum(file)
            self.cf.upload_file(self.cont_name,
                                file,
                                obj_name=secure_filename(file.filename),
                                etag=chksum)
            return True
        except pyrax.exceptions.UploadFailed:
            return False

    def _lookup_url(self, endpoint, values):
        """Return Rackspace URL for object."""
        try:
            cont = self.cf.get_container(self.cont_name)
            if cont.cdn_enabled:
                return "%s/%s" % (cont.cdn_uri, values['filename'])
        except:
            return None

Return URL for Rackspace CDN object