示例#1
0
    def upload(self, screenshot, name):
        self.loadSettings()
        #Save to memory buffer
        ba = QByteArray()
        buf = QBuffer(ba)
        buf.open(QIODevice.ReadWrite)
        screenshot.save(buf, ScreenCloud.getScreenshotFormat())
        #Create folder if not exists
        folders = self.driveService.files().list(
            q="name='%s' and mimeType='application/vnd.google-apps.folder' and trashed=false"
            % (self.folderName)).execute()["files"]
        exists = len(folders) > 0
        if not exists:
            folderMetadata = {
                'name': self.folderName,
                'mimeType': 'application/vnd.google-apps.folder'
            }
            folder = self.driveService.files().create(body=folderMetadata,
                                                      fields='id').execute()
        else:
            folder = folders[0]
        #Upload
        fileMetadata = {'name': name, 'parents': [folder["id"]]}
        media = MediaInMemoryUpload(ba.data(),
                                    mimetype='image/' +
                                    ScreenCloud.getScreenshotFormat())
        file = self.driveService.files().create(
            body=fileMetadata, media_body=media,
            fields='webViewLink, id').execute()
        if self.copyLink:
            webViewLink = file.get('webViewLink')
            ScreenCloud.setUrl(webViewLink)

        return True
示例#2
0
    def upload(self, screenshot, name):
        self.loadSettings()

        q_ba = QByteArray()
        q_buff = QBuffer(q_ba)

        q_buff.open(QIODevice.WriteOnly)
        screenshot.save(q_buff, ScreenCloud.getScreenshotFormat())
        q_buff.close()

        url = (self.host + '/upload').replace('//upload', '/upload')

        form = MultiPartForm()
        form.add_field('token', self.token)
        form.add_file('file', self.getFilename(), q_ba.data())
        data = bytes(form)

        r = urllib.request.Request(url,
                                   data,
                                   headers={
                                       'Content-Type': form.get_content_type(),
                                       'Content-Length': len(data),
                                       'User-Agent':
                                       'XBackBone/Screencloud-client'
                                   })

        try:
            res = urllib.request.urlopen(r)
            response = json.loads(res.read().decode('utf-8'))
            url = response.get('url')

            if not url:
                raise Exception(response.get('message'))

            ScreenCloud.setUrl(url)

        except urllib.error.HTTPError as e:
            response = json.loads(e.read())
            ScreenCloud.setError('Error while connecting to: ' + self.host +
                                 '\n' + response.get('message'))
            return False

        except Exception as e:
            try:
                ScreenCloud.setError('Could not upload to: ' + self.host +
                                     '\nError: ' + str(e))
            except AttributeError:
                ScreenCloud.setError('Unexpected error while uploading:\n' +
                                     traceback.format_exc())
            return False

        return True