ABAPSTEMP 07

  1. CLASS zini_cl_mime DEFINITION.
  2.  
  3.   PUBLIC SECTION.
  4.  
  5.     CONSTANTS:c_version TYPE string VALUE 'v01.00.20250403'.
  6.  
  7.     DATA: t_files TYPE STANDARD TABLE OF string.
  8.  
  9.     METHODS: constructor IMPORTING i_data TYPE any,
  10.       files_list,
  11.       file_put IMPORTING i_name    TYPE string
  12.                          i_upload  TYPE xfeld OPTIONAL
  13.                          x_content TYPE xstring OPTIONAL,
  14.       file_delete IMPORTING i_name TYPE string,
  15.       file_get IMPORTING i_data           TYPE any
  16.                RETURNING VALUE(x_content) TYPE xstring.
  17.  
  18.   PRIVATE SECTION.
  19.     CONSTANTS c_root TYPE string VALUE '/SAP/BC/BSP/SAP/'.
  20.     DATA: mime_api TYPE REF TO if_mr_api,
  21.           g_root   TYPE string.
  22.  
  23.     METHODS: file_get_upload RETURNING VALUE(x_content) TYPE xstring,
  24.       sample.
  25.  
  26. ENDCLASS.
  27.  
  28. CLASS zini_cl_mime IMPLEMENTATION.
  29.  
  30.   METHOD file_delete.
  31.     CALL METHOD mime_api->delete
  32.       EXPORTING
  33.         i_url              = |{ c_root }{ g_root }/{ i_name }|
  34. *       i_delete_children  = ''
  35. *       i_check_authority  = 'X'
  36. *       i_corr_number      =
  37.         i_suppress_dialogs = abap_true
  38.       EXCEPTIONS
  39.         parameter_missing  = 1
  40.         error_occured      = 2
  41.         cancelled          = 3
  42.         permission_failure = 4
  43.         not_found          = 5
  44.         OTHERS             = 6.
  45.   ENDMETHOD.
  46.  
  47.   METHOD file_put.
  48.     DATA: l_content TYPE xstring.
  49.     l_content = x_content.
  50.     IF i_upload IS INITIAL AND l_content IS INITIAL.
  51.       EXIT.
  52.     ENDIF.
  53.  
  54.     IF i_upload IS NOT INITIAL.
  55.       l_content = file_get_upload( ).
  56.     ENDIF.
  57.  
  58.     CHECK l_content IS NOT INITIAL.
  59.     CALL METHOD mime_api->put
  60.       EXPORTING
  61.         i_url                     = |{ c_root }{ g_root }/{ i_name }|
  62.         i_content                 = l_content
  63.         i_language                = sy-langu
  64.         i_description             = 'Subido desde programa'
  65. *       i_check_authority         = 'X'
  66.         i_suppress_package_dialog = abap_true
  67.         i_dev_package             = '$TMP'
  68.         i_genflag                 = abap_true
  69. *       i_corr_number             =
  70. *       i_new_loio                =
  71.         i_suppress_dialogs        = abap_true
  72. *       i_virus_profile           = C_VIRUS_PROFILE
  73.       EXCEPTIONS
  74.         parameter_missing         = 1
  75.         error_occured             = 2
  76.         cancelled                 = 3
  77.         permission_failure        = 4
  78.         data_inconsistency        = 5
  79.         new_loio_already_exists   = 6
  80.         is_folder                 = 7
  81.         OTHERS                    = 8.
  82.  
  83.     files_list( ).
  84.   ENDMETHOD.
  85.  
  86.   METHOD file_get_upload.
  87.     DATA: t_bin   TYPE STANDARD TABLE OF ssfbin,
  88.           t_lista TYPE filetable,
  89.           g_rc    TYPE sysubrc.
  90.     REFRESH t_bin.
  91.     CALL METHOD cl_gui_frontend_services=>file_open_dialog
  92.       EXPORTING
  93.         window_title            = 'Fichero a cargar'
  94. *       default_extension       =
  95. *       default_filename        =
  96. *       file_filter             =
  97. *       with_encoding           =
  98. *       initial_directory       =
  99.         multiselection          = abap_false
  100.       CHANGING
  101.         file_table              = t_lista
  102.         rc                      = g_rc
  103. *       user_action             =
  104. *       file_encoding           =
  105.       EXCEPTIONS
  106.         file_open_dialog_failed = 1
  107.         cntl_error              = 2
  108.         error_no_gui            = 3
  109.         not_supported_by_gui    = 4
  110.         OTHERS                  = 5.
  111.  
  112.     IF sy-subrc EQ 0.
  113.       LOOP AT t_lista INTO DATA(l_lista).
  114.         CALL METHOD cl_gui_frontend_services=>gui_upload
  115.           EXPORTING
  116.             filename                = CONV string( l_lista-filename )
  117.             filetype                = 'BIN'
  118. *           has_field_separator     = SPACE
  119. *           header_length           = 0
  120. *           read_by_line            = 'X'
  121. *           dat_mode                = SPACE
  122. *           codepage                = SPACE
  123. *           ignore_cerr             = ABAP_TRUE
  124. *           replacement             = '#'
  125. *           virus_scan_profile      =
  126.           IMPORTING
  127.             filelength              = DATA(l_size)
  128. *           header                  =
  129.           CHANGING
  130.             data_tab                = t_bin
  131. *           isscanperformed         = SPACE
  132.           EXCEPTIONS
  133.             file_open_error         = 1
  134.             file_read_error         = 2
  135.             no_batch                = 3
  136.             gui_refuse_filetransfer = 4
  137.             invalid_type            = 5
  138.             no_authority            = 6
  139.             unknown_error           = 7
  140.             bad_data_format         = 8
  141.             header_not_allowed      = 9
  142.             separator_not_allowed   = 10
  143.             header_too_long         = 11
  144.             unknown_dp_error        = 12
  145.             access_denied           = 13
  146.             dp_out_of_memory        = 14
  147.             disk_full               = 15
  148.             dp_timeout              = 16
  149.             not_supported_by_gui    = 17
  150.             error_no_gui            = 18
  151.             OTHERS                  = 19.
  152.         IF sy-subrc EQ 0.
  153.           CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
  154.             EXPORTING
  155.               input_length = l_size
  156. *             FIRST_LINE   = 0
  157. *             LAST_LINE    = 0
  158.             IMPORTING
  159.               buffer       = x_content
  160.             TABLES
  161.               binary_tab   = t_bin
  162.             EXCEPTIONS
  163.               failed       = 1
  164.               OTHERS       = 2.
  165.           IF sy-subrc <> 0.
  166.             CLEAR x_content.
  167.           ENDIF.
  168.  
  169.         ENDIF.
  170.  
  171.       ENDLOOP.
  172.     ENDIF.
  173.  
  174.   ENDMETHOD.
  175.  
  176.  
  177.   METHOD constructor.
  178.     mime_api = cl_mime_repository_api=>get_api( ).
  179.     g_root = i_data.
  180.   ENDMETHOD.
  181.  
  182.   METHOD files_list.
  183.     mime_api->file_list( EXPORTING i_url = |{ c_root }{ g_root }| IMPORTING e_files = t_files ).
  184.   ENDMETHOD.
  185.  
  186.   METHOD file_get.
  187.     CLEAR x_content.
  188.     CALL METHOD mime_api->get
  189.       EXPORTING
  190.         i_url              = CONV string( i_data )
  191.       IMPORTING
  192.         e_content          = x_content
  193. *       e_content_last_changed =
  194. *       e_mime_type        =
  195. *       e_loio             =
  196.       EXCEPTIONS
  197.         parameter_missing  = 1
  198.         error_occured      = 2
  199.         not_found          = 3
  200.         permission_failure = 4
  201.         OTHERS             = 5.
  202.   ENDMETHOD.
  203.  
  204.   METHOD sample.
  205. *--[ Hay que crear VIA SE80 en "Aplicación BSP" un App BSP
  206. *--[ Y allí se pueden colgar todo tipo de objetos
  207.     DATA(mi_obj) = NEW zini_cl_mime( 'ZROMERO' ).
  208.     mi_obj->file_put( i_name = 'TORERO.pdf' i_upload = abap_true ).
  209. *mi_obj->FILE_DELETE( i_name = 'TORERO.pdf' ).
  210.     mi_obj->files_list( ).
  211.     LOOP AT mi_obj->t_files INTO DATA(ll).
  212.       DATA(l_content) = mi_obj->file_get( ll ).
  213.     ENDLOOP.
  214.   ENDMETHOD.
  215.  
  216. ENDCLASS.