コード例 #1
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)
コード例 #2
0
	def upload(self, file, 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(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)
		
		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
	
		boundary = '----%s----' % ''.join((random.choice(
			'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') 
			for i in xrange(32)))
		data_header = []
		for name, value in predata.iteritems():
			data_header.append('--' + boundary) 
			data_header.append('Content-Disposition: form-data; name="%s"' % name)
			data_header.append('')
			data_header.append(value.encode('utf-8'))
			
		data_header.append('--' + boundary) 
		data_header.append('Content-Disposition: form-data; name="wpUploadFile"; filename="%s"' % \
			filename.encode('utf-8'))
		data_header.append('Content-Type: application/octet-stream')
		data_header.append('')
		data_header.append('')
		
		postdata = '\r\n'.join(data_header)
		content_length = (len(postdata) + file_size + 
				2 + # \r\n
				(6 + len(boundary)) +
				49 + # wpUpload
				2 + # \r\n 
				1 + # 1
				(4 + len(boundary)) + 
				2)
		
		def iterator():
			yield postdata
			while True:
				chunk = file.read(32768)
				if not chunk: break
				yield chunk
			yield '\r\n'
			
			yield '--%s\r\n' % boundary
			yield 'Content-Disposition: form-data; name="wpUpload"\r\n'
			yield '\r\n'
			yield '1'
			
			yield '--%s--' % boundary
			yield '\r\n'
		
		wait_token = self.wait_token()
		while True:
			try:
				self.connection.post(self.host,
					self.path + 'index.php?title=Special:Upload&maxlag=' + self.max_lag,
					headers = {'Content-Type': 'multipart/form-data; boundary=' + boundary,
						'Content-Length': str(content_length)},
					stream_iter = iterator()).read()
			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)