S004 file - copy remote to remote - bin mode

########################################################;
# Copy a remote file to a remote file (in binary mode) #;
########################################################;

#Initialization;
-> "[source]" "/Users/jimmitry/Desktop/test.jpg";
-> "[destination]" "/Users/jimmitry/Desktop/copy.jpg";
-> "[nb_bytes]" 204800;

try {

    #Connect the remote MentDB servers;
    tunnel connect "session1" {cm get "demo_cm_mentdb1";};
    tunnel connect "session2" {cm get "demo_cm_mentdb2";};

    #Open a remote reader;
    tunnel execute "session1" (concat 
        "-> \"[source]\" \"" (mql encode [source]) "\";"
        (mql {
            file reader_open "r1" [source] BINARY null;
        })
    );

    #Open a remote file;
    tunnel execute "session2" (concat 
        "-> \"[destination]\" \"" (mql encode [destination]) "\";"
        (mql {
            file writer_open "w1" [destination] true BINARY null;
        })
    );

    #Parse the file;
    while (is not null (-> "[bytes]" 
        (tunnel execute "session1" (concat 
            "-> \"[nb_bytes]\" \"" (mql encode [nb_bytes]) "\";"
            (mql {
                file reader_get_bytes "r1" [nb_bytes]
            })
        )))) {

        #Send to the remote server the bytes;
        tunnel execute "session2" (concat 
            "-> \"[bytes]\" \"" (mql encode [bytes]) "\";"
            (mql {
                file writer_add_bytes "w1" [bytes];
            })
        );

    };

    #Force to write and close the remote writer;
    tunnel execute "session2" (concat 
        (mql {
            file writer_flush "w1";
            file writer_close "w1";
        })
    );

    #Close the remote reader;
    tunnel execute "session1" "file reader_close \"r1\";";

    #Disconnect from the remote MentDB servers;
    tunnel disconnect "session1";
    tunnel disconnect "session2";

} {

    #Close objects;
    try {tunnel disconnect "session1";} {} "[sub_err]";
    try {tunnel disconnect "session2";} {} "[sub_err]";
    try {file reader_close "r1";} {} "[sub_err]";
    try {file writer_close "w1";} {} "[sub_err]";

    #Generate an error;
    exception (1) ([err]);

} "[err]";


MentDB © 2012 - 2020 - Legal Notice