CLASS zini_cl_mime DEFINITION.
PUBLIC SECTION.
CONSTANTS:c_version TYPE string VALUE 'v01.00.20250403'.
DATA: t_files TYPE STANDARD TABLE OF string.
METHODS: constructor IMPORTING i_data TYPE any,
files_list,
file_put IMPORTING i_name TYPE string
i_upload TYPE xfeld OPTIONAL
x_content TYPE xstring OPTIONAL,
file_delete IMPORTING i_name TYPE string,
file_get IMPORTING i_data TYPE any
RETURNING VALUE(x_content) TYPE xstring.
PRIVATE SECTION.
CONSTANTS c_root TYPE string VALUE '/SAP/BC/BSP/SAP/'.
DATA: mime_api TYPE REF TO if_mr_api,
g_root TYPE string.
METHODS: file_get_upload RETURNING VALUE(x_content) TYPE xstring,
sample.
ENDCLASS.
CLASS zini_cl_mime IMPLEMENTATION.
METHOD file_delete.
CALL METHOD mime_api->delete
EXPORTING
i_url = |{ c_root }{ g_root }/{ i_name }|
* i_delete_children = ''
* i_check_authority = 'X'
* i_corr_number =
i_suppress_dialogs = abap_true
EXCEPTIONS
parameter_missing = 1
error_occured = 2
cancelled = 3
permission_failure = 4
not_found = 5
OTHERS = 6.
ENDMETHOD.
METHOD file_put.
DATA: l_content TYPE xstring.
l_content = x_content.
IF i_upload IS INITIAL AND l_content IS INITIAL.
EXIT.
ENDIF.
IF i_upload IS NOT INITIAL.
l_content = file_get_upload( ).
ENDIF.
CHECK l_content IS NOT INITIAL.
CALL METHOD mime_api->put
EXPORTING
i_url = |{ c_root }{ g_root }/{ i_name }|
i_content = l_content
i_language = sy-langu
i_description = 'Subido desde programa'
* i_check_authority = 'X'
i_suppress_package_dialog = abap_true
i_dev_package = '$TMP'
i_genflag = abap_true
* i_corr_number =
* i_new_loio =
i_suppress_dialogs = abap_true
* i_virus_profile = C_VIRUS_PROFILE
EXCEPTIONS
parameter_missing = 1
error_occured = 2
cancelled = 3
permission_failure = 4
data_inconsistency = 5
new_loio_already_exists = 6
is_folder = 7
OTHERS = 8.
files_list( ).
ENDMETHOD.
METHOD file_get_upload.
DATA: t_bin TYPE STANDARD TABLE OF ssfbin,
t_lista TYPE filetable,
g_rc TYPE sysubrc.
REFRESH t_bin.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Fichero a cargar'
* default_extension =
* default_filename =
* file_filter =
* with_encoding =
* initial_directory =
multiselection = abap_false
CHANGING
file_table = t_lista
rc = g_rc
* user_action =
* file_encoding =
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc EQ 0.
LOOP AT t_lista INTO DATA(l_lista).
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = CONV string( l_lista-filename )
filetype = 'BIN'
* has_field_separator = SPACE
* header_length = 0
* read_by_line = 'X'
* dat_mode = SPACE
* codepage = SPACE
* ignore_cerr = ABAP_TRUE
* replacement = '#'
* virus_scan_profile =
IMPORTING
filelength = DATA(l_size)
* header =
CHANGING
data_tab = t_bin
* isscanperformed = SPACE
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19.
IF sy-subrc EQ 0.
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = l_size
* FIRST_LINE = 0
* LAST_LINE = 0
IMPORTING
buffer = x_content
TABLES
binary_tab = t_bin
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
CLEAR x_content.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
ENDMETHOD.
METHOD constructor.
mime_api = cl_mime_repository_api=>get_api( ).
g_root = i_data.
ENDMETHOD.
METHOD files_list.
mime_api->file_list( EXPORTING i_url = |{ c_root }{ g_root }| IMPORTING e_files = t_files ).
ENDMETHOD.
METHOD file_get.
CLEAR x_content.
CALL METHOD mime_api->get
EXPORTING
i_url = CONV string( i_data )
IMPORTING
e_content = x_content
* e_content_last_changed =
* e_mime_type =
* e_loio =
EXCEPTIONS
parameter_missing = 1
error_occured = 2
not_found = 3
permission_failure = 4
OTHERS = 5.
ENDMETHOD.
METHOD sample.
*--[ Hay que crear VIA SE80 en "Aplicación BSP" un App BSP
*--[ Y allí se pueden colgar todo tipo de objetos
DATA(mi_obj) = NEW zini_cl_mime( 'ZROMERO' ).
mi_obj->file_put( i_name = 'TORERO.pdf' i_upload = abap_true ).
*mi_obj->FILE_DELETE( i_name = 'TORERO.pdf' ).
mi_obj->files_list( ).
LOOP AT mi_obj->t_files INTO DATA(ll).
DATA(l_content) = mi_obj->file_get( ll ).
ENDLOOP.
ENDMETHOD.
ENDCLASS.