# -*- coding: UTF-8 -*-
# =========================================================================
# Copyright (C) 2017 Yunify, Inc.
# -------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this work except in compliance with the License.
# You may obtain a copy of the License in the LICENSE file, or at:
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========================================================================

from __future__ import unicode_literals

import sys

from .base import BaseCommand
from ..utils import get_current_time, uni_print


class PresignCommand(BaseCommand):

    command = "presign"
    usage = "%(prog)s <qs-path> [-e <expire_seconds>]"

    @classmethod
    def add_extra_arguments(cls, parser):

        parser.add_argument(
            "qs_path",
            nargs="?",
            default="qs://",
            help="The qs-path to presign"
        )

        parser.add_argument(
            "-e",
            "--expire",
            dest="expire_seconds",
            type=int,
            default=3600,
            help="The number of seconds until the pre-signed URL expires."
        )
        return parser

    @classmethod
    def generate_presign_url(cls):
        bucket, prefix = cls.validate_qs_path(cls.options.qs_path)
        if prefix == "":
            uni_print("Error: please specify object in qs-path")
            sys.exit(-1)
        cls.validate_bucket(bucket)
        current_bucket = cls.client.Bucket(bucket, cls.bucket_map[bucket])
        prepared = current_bucket.get_object_request(prefix).sign_query(
            get_current_time() + cls.options.expire_seconds
        )
        uni_print(prepared.url)
        return prepared.url

    @classmethod
    def send_request(cls):
        cls.generate_presign_url()

@@ -1014,17 +1014,16 @@
 mmand):%0A
-%0A
     comm
@@ -1930,73 +1930,1304 @@
     
-prepared = current_bucket.get_object_request(prefix).sign_query(%0A
+resp = current_bucket.head_object(prefix)%0A        if resp.status_code == 404:%0A            uni_print(%22Error: Please check if object %3C%25s%3E exists%22 %25 prefix)%0A            sys.exit(-1)%0A        elif resp.status_code == 403:%0A            uni_print(%0A                %22Error: Please check if you have enough%22%0A                %22 permission to access object %3C%25s%3E.%22 %25 prefix%0A            )%0A            sys.exit(-1)%0A        elif resp.status_code == 200:%0A            current_acl = current_bucket.get_acl()%0A            for v in current_acl%5B%22acl%22%5D:%0A                if v%5B%22grantee%22%5D%5B%22name%22%5D == %22QS_ALL_USERS%22:  # check whether the bucket is public%0A                    public_url = %22%7Bprotocol%7D://%7Bbucket_name%7D.%7Bzone%7D.%7Bhost%7D/%7Bobject_key%7D%22.format(%0A                        protocol=current_bucket.config.protocol,%0A                        bucket_name=bucket,%0A                        zone=cls.bucket_map%5Bbucket%5D,%0A                        host=current_bucket.config.host,%0A                        object_key=prefix%0A                    )%0A                    uni_print(public_url)%0A                    return public_url%0A            prepared = current_bucket.get_object_request(prefix).sign_query(  # if the bucket is non-public,%0A                # generate the link with signature, expire seconds and other formatted parameters%0A    
     
@@ -3282,32 +3282,36 @@
 seconds%0A        
+    
 )%0A        uni_pr
@@ -3296,32 +3296,36 @@
       )%0A        
+ 
+   
 uni_print(prepar
@@ -3332,32 +3332,36 @@
 ed.url)%0A        
+    
 return prepared.
@@ -3363,16 +3363,91 @@
 ared.url
+%0A        else:%0A            uni_print(resp.content)%0A            sys.exit(-1)
 %0A%0A    @c

Update commond qsctl-presign