@@ -390,7 +390,7 @@ def add_comment(self, page_id, text):
390
390
'body' : self ._create_body (text , 'storage' )}
391
391
return self .post ('rest/api/content/' , data = data )
392
392
393
- def attach_file (self , filename , page_id = None , title = None , space = None , comment = None ):
393
+ def attach_content (self , content , name , content_type = 'application/binary' , page_id = None , title = None , space = None , comment = None ):
394
394
"""
395
395
Attach (upload) a file to a page, if it exists it will update the
396
396
automatically version the new file and keep the old one.
@@ -400,22 +400,22 @@ def attach_file(self, filename, page_id=None, title=None, space=None, comment=No
400
400
:type space: ``str``
401
401
:param page_id: The page id to which we would like to upload the file
402
402
:type page_id: ``str``
403
- :param filename: The file to upload
404
- :type filename: ``str``
403
+ :param name: The name of the attachment
404
+ :type name: ``str``
405
+ :param content: Contains the content which should be uplaoded
406
+ :type content: ``binary``
407
+ :param content_type: Specify the HTTP content type. The default is
408
+ :type content_type: ``str``
405
409
:param comment: A comment describing this upload/file
406
410
:type comment: ``str``
407
411
"""
408
412
page_id = self .get_page_id (space = space , title = title ) if page_id is None else page_id
409
413
type = 'attachment'
410
414
if page_id is not None :
411
- # get base name of the file to get the attachment from confluence.
412
- file_base_name = os .path .basename (filename )
413
- extension = os .path .splitext (filename )[- 1 ]
414
- content_type = self .content_types .get (extension , "application/binary" )
415
- comment = comment if comment else "Uploaded {filename}." .format (filename = filename )
415
+ comment = comment if comment else "Uploaded {filename}." .format (filename = name )
416
416
data = {
417
417
'type' : type ,
418
- "fileName" : file_base_name ,
418
+ "fileName" : name ,
419
419
"contentType" : content_type ,
420
420
"comment" : comment ,
421
421
"minorEdit" : "true" }
@@ -424,16 +424,38 @@ def attach_file(self, filename, page_id=None, title=None, space=None, comment=No
424
424
'Accept' : 'application/json' }
425
425
path = 'rest/api/content/{page_id}/child/attachment' .format (page_id = page_id )
426
426
# Check if there is already a file with the same name
427
- attachments = self .get (path = path , headers = headers , params = {'filename' : file_base_name })
427
+ attachments = self .get (path = path , headers = headers , params = {'filename' : name })
428
428
if attachments ['size' ]:
429
429
path = path + '/' + attachments ['results' ][0 ]['id' ] + '/data'
430
- with open (filename , 'rb' ) as infile :
431
- return self .post (path = path , data = data , headers = headers ,
432
- files = {'file' : (file_base_name , infile , content_type )})
430
+ return self .post (path = path , data = data , headers = headers ,
431
+ files = {'file' : (name , content , content_type )})
433
432
else :
434
433
log .warning ("No 'page_id' found, not uploading attachments" )
435
434
return None
436
435
436
+ def attach_file (self , filename , page_id = None , title = None , space = None , comment = None ):
437
+ """
438
+ Attach (upload) a file to a page, if it exists it will update the
439
+ automatically version the new file and keep the old one.
440
+ :param title: The page name
441
+ :type title: ``str``
442
+ :param space: The space name
443
+ :type space: ``str``
444
+ :param page_id: The page id to which we would like to upload the file
445
+ :type page_id: ``str``
446
+ :param filename: The file to upload
447
+ :type filename: ``str``
448
+ :param comment: A comment describing this upload/file
449
+ :type comment: ``str``
450
+ """
451
+ # get base name of the file to get the attachment from confluence.
452
+ name = os .path .basename (filename )
453
+ extension = os .path .splitext (filename )[- 1 ]
454
+ content_type = self .content_types .get (extension , "application/binary" )
455
+ with open (filename , 'rb' ) as infile :
456
+ content = infile .read ()
457
+ return self .attach_content ( content , name , content_type , page_id = page_id , title = title , space = space , comment = comment )
458
+
437
459
def delete_attachment (self , page_id , filename , version = None ):
438
460
"""
439
461
Remove completely a file if version is None or delete version
0 commit comments