# coding: utf-8
"""
    flask_storage.local
    ~~~~~~~~~~~~~~~~~~~

    Local storage, save the file in local directory.

    :copyright: (c) 2013 Hsiaoming Yang.
"""

import os
from urlparse import urljoin
from ._base import BaseStorage, UploadFileExists


class LocalStorage(BaseStorage):
    @property
    def root(self):
        return self.config.get('STORAGE_LOCAL_ROOT')

    def url(self, filename):
        """Generate the url for a filename.

        :param filename: filename for generating the url....
        """
        urlbase = self.config.get('STORAGE_LOCAL_URL')
        return urljoin(urlbase, filename)

    def exists(self, filename):
        """Detect if the file exists.

        :param filename: name of the file.
        """
        dest = os.path.join(self.root, filename)
        return os.path.exists(dest)

    def read(self, filename):
        """Read content of a file.
        """
        dest = os.path.join(self.root, filename)
        with open(dest) as f:
            return f.read()

    def delete(self, filename):
        """Delete the specified file.

        :param filename: name of the file.
        """
        dest = os.path.join(self.root, filename)
        return os.remove(dest)

    def save(self, storage, filename):
        """Save a storage (`werkzeug.FileStorage`) with the specified
        filename.

        :param storage: The storage to be saved.
        :param filename: The destination of the storage.
        """
        self.check(storage)
        dest = os.path.join(self.root, filename)

        folder = os.path.dirname(dest)
        if not os.path.exists(folder):
            os.makedirs(folder)

        if os.path.exists(dest):
            raise UploadFileExists()

        storage.save(dest)
        return self.url(filename)

@@ -890,37 +890,28 @@
 t of a file.
-%0A        
 %22%22%22%0A
+
         dest
@@ -977,16 +977,16 @@
 ) as f:%0A
-
         
@@ -1002,24 +1002,337 @@
 n f.read()%0A%0A
+    def write(self, filename, content):%0A        %22%22%22Write content to a file.%22%22%22%0A        dest = os.path.join(self.root, filename)%0A        dirname = os.path.dirname(dest)%0A        if not os.path.exists(dirname):%0A            os.makedirs(dirname)%0A        with open(dest, 'w') as f:%0A            return f.write(content)%0A%0A
     def dele

add write API for local