Jose Renato Pinto

Forum Replies Created

Viewing 4 posts - 1 through 4 (of 4 total)
in reply to: How to upload the files of a package using REST API #163134

Jose Renato Pinto
Participant

Hey Nayeem.

I didnt undertand what you asked.

in reply to: How to upload the files of a package using REST API #162925

Jose Renato Pinto
Participant

I’m not sure if I missed something… maybe I am…. because I still not able to upload files to this folder using a Java Client with REST API.

Maybe you are suggesting me to upload the files to wp-content/uploads/download-manager-files via FTP? Is that it?

Because reading your answer, I’m not able to find any clue about how to upload the files to this folder.

I’m sorry for beeing this persistent, but I’m realy need to understend how this works.

I was expecting some code like this to upload the files:


import java.io.File;
import java.io.IOException;
 
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
 
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
 
public class JerseyFileUpload {
    private final static String contentType = "multipart/mixed";
 
	public static void test(){
		postFile("http://mypage.com/wp-json/wpdm/v1/package", "c:\\TEMP\\File.jpg");
	}
	
    public static void postFile(String serverURL, File imgFile) {
        MultiPart multiPart = null;
        try {
            Client client = ClientBuilder.newBuilder().
                                register(MultiPartFeature.class).build();
            WebTarget server = client.target(serverURL);
            multiPart = new MultiPart();
 
           
            FileDataBodyPart imgBodyPart = new FileDataBodyPart("imgFile", imgFile,
                    MediaType.APPLICATION_OCTET_STREAM_TYPE);
           
            // Add body part           
            multiPart.bodyPart(imgBodyPart);            
 
            Response response = server.request(MediaType.APPLICATION_JSON_TYPE)
                    .post(Entity.entity(multiPart, contentType));
            if (response.getStatus() == 200) {
                String respnse = response.readEntity(String.class);
                System.out.println(respnse);
            } else {
                System.out.println("Response is not ok");
            }
        } catch (Exception e) {
            System.out.println("Exception has occured "+ e.getMessage());
        } finally {
            if (null != multiPart) {
                try {
                    multiPart.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
in reply to: How to upload the files of a package using REST API #162829

Jose Renato Pinto
Participant

Hey Nayeen.

Thanks for the attention.

Thats the main problem… I cant upload files using Java and REST API to this location.

This path (wp-content/uploads/download-manager-files), how can I, programmatically, upload files to it via REST API? Is it possible? Do yout have any code sample showing how to do it?

in reply to: How to upload the files of a package using REST API #162695

Jose Renato Pinto
Participant

Hello Nayeem.

I did not understood how I’m gona to attach the files.

I alread saw the “files” object and could not use it for uploading files, only to make reference to files that were alread uploaded previously (that are on the Media of Worpress).

The description of “files” object is: “Array of attached file names key by file ids.”

What kind of “file names” I’m supoused to put there? The local path of my PC? (like C:\TEMP\file_to_upload.jpg)

And what about the “id”? What kind of Id?

Do you have any example to give me?

I alread tried to put the local path there (like the above example), but the files were not uploaded to the server.

Here is a snippet code of what I’m doing:


import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation.Builder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

public class Test {

	public String createPackage( final String name, final long categoryId, final String fileName ) {
		try {
			final Map params = new HashMap<>();
			params.put( "title", name ); //$NON-NLS-1$

			final List<Long> categoryIdList = new ArrayList<>();
			categoryIdList.add( categoryId );
			params.put( "categories", categoryIdList ); //$NON-NLS-1$

			if ( null != fileName && ( !fileName.isEmpty() ) ) {
				final List<String> files = new ArrayList<>();
				files.add( fileName );
				params.put( "files", files ); //$NON-NLS-1$
			}

			params.put( "status", "private" ); //$NON-NLS-1$ //$NON-NLS-2$

			final List<String> access = new ArrayList<>();
			access.add( "guest" );
			params.put( "access", access ); //$NON-NLS-1$

			final Entity entry = Entity.json( params );

			final Client wpdmClient = ClientBuilder.newClient();
			WebTarget wpdmPackagesTarget = wpdmClient.target( wpdmPackagesUrl );
			String wpdmAuthorizationKey = "Bearer xxxxxxx"; //$NON-NLS-1$
			final Builder builder = wpdmPackagesTarget.request( MediaType.APPLICATION_JSON ).header(
				HttpHeaders.AUTHORIZATION, wpdmAuthorizationKey );

			final JSONObject response = builder.post( entry, JSONObject.class );

			return response.toJSONString();
		} catch ( Exception e ) {
			e.printStackTrace();
		}

		return null;
	}

}
Viewing 4 posts - 1 through 4 (of 4 total)