#NEWLINE# @ method_decorator ( login_or_digest_or_basic_or_apikey ( ) ) #NEWLINE# def get( self , request , domain , case_id = None , attachment_id = None ) : #NEWLINE# #INDENT# '\n        https://github.com/dimagi/commcare/wiki/CaseAttachmentAPI\n        max_size\tThe largest size (in bytes) for the attachment\n        max_image_width\tThe largest width in pixels for an an image attachment\n        max_image_height\tThe largest width in pixels for an an image attachment\n        ' #NEWLINE# if ( self . request . couch_user . is_web_user ( ) and ( not can_view_attachments ( self . request ) ) ) : #NEWLINE# #INDENT# return HttpResponseForbidden ( ) #NEWLINE# #UNINDENT# if ( ( not case_id ) or ( not attachment_id ) ) : #NEWLINE# #INDENT# raise Http404 #NEWLINE# #UNINDENT# img = self . request . GET . get ( 'img' , None ) #NEWLINE# size = self . request . GET . get ( 'size' , OBJECT_ORIGINAL ) #NEWLINE# max_width = int ( self . request . GET . get ( 'max_image_width' , 0 ) ) #NEWLINE# max_height = int ( self . request . GET . get ( 'max_image_height' , 0 ) ) #NEWLINE# max_filesize = int ( self . request . GET . get ( 'max_size' , 0 ) ) #NEWLINE# try : #NEWLINE# #INDENT# CaseAccessors ( domain ) . get_case ( case_id ) #NEWLINE# #UNINDENT# except CaseNotFound : #NEWLINE# #INDENT# raise Http404 #NEWLINE# #UNINDENT# if ( img is not None ) : #NEWLINE# #INDENT# if ( size == 'debug_all' ) : #NEWLINE# #INDENT# url_base = reverse ( 'api_case_attachment' , kwargs = { #NEWLINE# #INDENT# 'domain' : self . request . domain , #NEWLINE# 'case_id' : case_id , #NEWLINE# 'attachment_id' : attachment_id , #NEWLINE# #UNINDENT# } ) #NEWLINE# r = HttpResponse ( content_type = 'text/html' ) #NEWLINE# r . write ( '<html><body>' ) #NEWLINE# r . write ( '<ul>' ) #NEWLINE# for fsize in IMAGE_SIZE_ORDERING : #NEWLINE# #INDENT# ( meta , stream ) = fetch_case_image ( domain , case_id , attachment_id , filesize_limit = max_filesize , width_limit = max_width , height_limit = max_height , fixed_size = fsize ) #NEWLINE# r . write ( '<li>' ) #NEWLINE# r . write ( ( 'Size: %s<br>' % fsize ) ) #NEWLINE# r . write ( ( 'Limit: max_size: %s' % max_filesize ) ) #NEWLINE# if ( max_width > 0 ) : #NEWLINE# #INDENT# r . write ( ( ', max_width: %s' % max_width ) ) #NEWLINE# #UNINDENT# if ( max_height > 0 ) : #NEWLINE# #INDENT# r . write ( ( ', max_height: %s' % max_height ) ) #NEWLINE# #UNINDENT# r . write ( '<br>' ) #NEWLINE# if ( meta is not None ) : #NEWLINE# #INDENT# r . write ( ( 'Resolution: %d x %d<br>' % ( meta [ 'width' ] , meta [ 'height' ] ) ) ) #NEWLINE# r . write ( ( 'Filesize: %d<br>' % meta [ 'content_length' ] ) ) #NEWLINE# url_params = urllib . urlencode ( { #NEWLINE# #INDENT# 'img' : '1' , #NEWLINE# 'size' : case_id , #NEWLINE# 'max_size' : max_filesize , #NEWLINE# 'max_image_width' : max_width , #NEWLINE# 'max_image_height' : max_height , #NEWLINE# #UNINDENT# } ) #NEWLINE# r . write ( ( '<img src="%(attach_url)s?%(params)s">' % { #NEWLINE# #INDENT# 'attach_url' : url_base , #NEWLINE# 'params' : url_params , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# r . write ( 'Not available' ) #NEWLINE# #UNINDENT# r . write ( '</li>' ) #NEWLINE# #UNINDENT# r . write ( '</ul></body></html>' ) #NEWLINE# return r #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( attachment_meta , attachment_stream ) = fetch_case_image ( domain , case_id , attachment_id , filesize_limit = max_filesize , width_limit = max_width , height_limit = max_height , fixed_size = size ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# cached_attachment = get_cached_case_attachment ( domain , case_id , attachment_id ) #NEWLINE# ( attachment_meta , attachment_stream ) = cached_attachment . get ( ) #NEWLINE# #UNINDENT# if ( attachment_meta is not None ) : #NEWLINE# #INDENT# mime_type = attachment_meta [ 'content_type' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# mime_type = 'plain/text' #NEWLINE# #UNINDENT# return StreamingHttpResponse ( streaming_content = attachment_stream , content_type = mime_type )