Пример #1
0
 def post(self, command, service=None):
     ''' process POST request        
     '''
     
     if service == None:
         # select service adapter
         self.service, self.backend = self.detect_service()
         command = self.request.path_info_pop()
     
     # Check authorization
     user_auth = auth.valid_token(self.request)
     if user_auth == None:                    
         self.send_unauthorized()
         return
                  
     if command == 'files':            
         # pass whole file to user
         creds_key = user_auth.owner + "_" + self.service
         creds = ServiceData.get_by_key_name(creds_key)
         if creds == None:
             self.send_unauthorized('no authorization data for service '+self.service)
         else:                
             path = self.request.path_info
             # construct file-like object for backend
             import StringIO                
             # use raw post parameter
             buf = StringIO.StringIO(self.request.str_POST['file'])
             status = self.backend.put_file(path, creds.credentials, buf)
             self.response.set_status(status)                            
         return
     
     if command == 'share':
         # add public link
         # call addshare handler
         self.get('addshare', self.service)
Пример #2
0
    def get(self, command, service=None):
        ''' process GET request        
        '''
                
        if service == None:
            # select service adapter
            self.service, self.backend = self.detect_service()
            command = self.request.path_info_pop()            
  
        # not all commands need authorization
        if self.check_public_commands(command):
            return        
                
        # Check authorization
        user_auth = auth.valid_token(self.request)
        if user_auth == None:
            self.send_unauthorized()
            return
                     
        if command == 'files':            
            # pass whole file to user
            creds_key = user_auth.owner + "_" + self.service
            creds = ServiceData.get_by_key_name(creds_key)
            if creds == None:
                self.send_unauthorized('no authorization data for service '+self.service)
            else:
                if self.request.get('version'):
                    params = {'version' : self.request.get('version')}
                else:
                    params = None
                path = self.request.path_info                
                status = self.backend.get_file(self.response.out, path, creds.credentials, params)
                self.response.set_status(status)
            return                    
            
        if command == 'metadata':            
            # return metadata
            creds_key = user_auth.owner + "_" + self.service
            creds = ServiceData.get_by_key_name(creds_key)

            if creds == None:                
                self.send_unauthorized('no authorization data for service '+self.service)
            else:
                if self.request.get('list'):
                    params = {'list' : True if self.request.get('list') == 'true' else False }
                else:
                    params = None              
                path = self.request.path_info
                status, metadata = self.backend.get_metadata(path, creds.credentials, params)
                # output json
                json.dump(metadata, self.response.out, ensure_ascii=False)                
                self.response.set_status(status)                            
            return
        
        if command == 'addshare':
            # create public link to file
            creds_key = user_auth.owner + "_" + self.service
            creds = ServiceData.get_by_key_name(creds_key)
            if creds == None:
                self.send_unauthorized('no authorization data for service '+self.service)
            else:
                path = self.request.path_info
                link_key = creds_key + path
                link = ShareLink.get_or_insert(link_key, data=creds, path=path)
                link.regenerate_link() # generate link code                
                self.response.out.write(link.link)
            return
        
        if command == 'share':
            # return list of shares
            # get credentials            
            creds_key = user_auth.owner + "_" + self.service
            creds = ServiceData.get_by_key_name(creds_key)
            if creds == None:
                self.send_unauthorized('no authorization data for service '+self.service)
            else:
                links = ShareLink.gql('WHERE data = :1', creds)
                link_list = [(link.path, link.link) for link in links]
                # return json
                json.dump(link_list, self.response.out, ensure_ascii=False)                                
            return
        
        if command == 'unshare':
            # delete public link to file
            # get credentials
            creds_key = user_auth.owner + "_" + self.service
            creds = ServiceData.get_by_key_name(creds_key)
            if creds == None:
                self.send_unauthorized('no authorization data for service '+self.service)
            else:
                path = self.request.path_info
                link_key = creds_key + path
                link = ShareLink.get_by_key_name(link_key)
                if link == None:
                    self.error(404)
                else: 
                    link.delete()
            return
                        
        # if we got down here then command is not recognized
        # report error
        self.error(400)            
        self.response.headers.add_header("Cache-Control", "no-store")                
        self.response.out.write("Unsupported command")