Esempio n. 1
0
    def post(self):
        # getting variable that include necessary informations for uploading file.
        upload = self.get_uploads()[0]

        # getting path
        root = self.session.get('root')
        path = self.request.POST.get('path')
        if path != '':
            file_path = root + '/' + path
        else:
            file_path = root

        # Get file info from blobinfo
        file_name = upload.filename

        # Check if it duplicates
        if checkDuplicatedFile(root, file_name):
            # Delete the file from blobstore
            blobstore.BlobInfo(upload.key()).delete()

            # Display error message
            self.redirect('/?path=' + path + '&err=File already exists.')
            return

        file_size = int(round(blobstore.BlobInfo(upload.key()).size / 1000))
        if file_size < 1:
            file_size = 1
        file_date = blobstore.BlobInfo(upload.key()).creation

        # When uploading file, if the file with same name and path is saved in blobstore, delete earlier file and rewrite that file
        query_result = File.query()
        query_result = query_result.filter(File.path == file_path)
        query_result = query_result.filter(File.name == file_name)
        result = query_result.fetch()
        if len(result) > 0:
            result[0].key.delete()
            blobstore.delete(result[0].blob_key)

        # file update to datastore
        file = File()
        file.name = file_name
        file.path = file_path
        file.blob_key = upload.key()
        file.size = file_size
        file.cdate = str(file_date.strftime("%m/%d/%Y %H:%M:%S"))
        file.put()

        self.redirect('/?path=' + path)