Esempio n. 1
0
	def upload(self, file = None, filename = None, description = '', ignore = False, file_size = None,
			url = None, session_key = None):
		if self.version[:2] < (1, 16):
			return compatibility.old_upload(self, file = file, filename = filename, 
						description = description, ignore = ignore, 
						file_size = file_size)
		
		image = self.Images[filename]
		if not image.can('upload'):
			raise errors.InsufficientPermission(filename)
		

		
		predata = {}
		
		predata['comment'] = description
		if ignore: 
			predata['ignorewarnings'] = 'true'
		predata['token'] = image.get_token('edit')
		predata['action'] = 'upload'
		predata['format'] = 'json'
		predata['filename'] = filename
		if url:
			predata['url'] = url
		if session_key:
			predata['session_key'] = session_key 
		
		if file is None:
			postdata = predata
		else:			
			if type(file) is str:
				file_size = len(file)
				file = StringIO(file)
			if file_size is None:
				file.seek(0, 2)
				file_size = file.tell()
				file.seek(0, 0)
				
			postdata = upload.UploadFile('file', filename, file_size, file, predata)
		
		wait_token = self.wait_token()
		while True:
			try:
				data = self.raw_call('api', postdata).read()
				info = json.loads(data)
				if not info:
					info = {}
				if self.handle_api_result(info, kwargs = predata):
					return info.get('upload', {})
			except errors.HTTPStatusError, e:
				if e[0] == 503 and e[1].getheader('X-Database-Lag'):
					self.wait(wait_token, int(e[1].getheader('Retry-After')))
				elif e[0] < 500 or e[0] > 599:
					raise
				else:
					self.wait(wait_token)
			except errors.HTTPError:
				self.wait(wait_token)
Esempio n. 2
0
def old_upload(self,
               fileobj,
               filename,
               description,
               license='',
               ignore=False,
               file_size=None):
    image = self.Images[filename]
    if not image.can('upload'):
        raise errors.InsufficientPermission(filename)
    if image.exists and not ignore:
        raise errors.FileExists(filename)

    if type(fileobj) is str:
        file_size = len(fileobj)
        fileobj = StringIO(fileobj)
    if file_size is None:
        fileobj.seek(0, 2)
        file_size = fileobj.tell()
        fileobj.seek(0, 0)

    predata = {}
    # Do this thing later so that an incomplete upload won't work
    # predata['wpDestFile'] = filename
    predata['wpUploadDescription'] = description
    predata['wpLicense'] = license
    if ignore:
        predata['wpIgnoreWarning'] = 'true'
    predata['wpUpload'] = 'Upload file'
    predata['wpSourceType'] = 'file'
    predata['wpDestFile'] = filename
    predata['wpEditToken'] = image.get_token('edit')

    postdata = upload.UploadFile('wpUploadFile', filename, file_size, fileobj,
                                 predata)

    wait_token = self.wait_token()
    while True:
        try:
            self.connection.post(self.host,
                                 '%sindex.php?title=Special:Upload&maxlag=%s' %
                                 (self.path, self.max_lag),
                                 data=postdata).read()
        except errors.HTTPStatusError as exc:
            e = exc.args if pythonver >= 3 else exc
            if e[0] == 503 and e[1].getheader('X-Database-Lag'):
                self.wait(wait_token, int(e[1].getheader('Retry-After')))
            elif e[0] < 500 or e[0] > 599:
                raise
            else:
                self.wait(wait_token)
        except errors.HTTPError:
            self.wait(wait_token)
        else:
            return
        fileobj.seek(0, 0)