create attachment with Rest API

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

create attachment with Rest API

zacharykane
Hello, I'm trying to create an attachment on a page from a URL to an image, using JavaScript and the RESTful API.

I'm not sure I'm doing it correctly of if this can be done but here's where I'm at:

httpRequest.open('PUT', '/xwiki/rest/wikis/XWiki/spaces/Main/pages/Daily All-Source Digest – 2013-01-05T07:30P24/attachments/Blade Header UnClassified-S.png');

httpRequest.setRequestHeader('Content-Type', 'application/xml');

httpRequest.send('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><attachment xmlns="http://www.xwiki.org"><name>"Blade Header UnClassified-S.png"</name><mimeType>"image/png"</mimeType><xwikiAbsoluteUrl>"https://domain/xwiki/bin/download/ColorThemes/Nightfall/Blade%20Header%20UnClassified-S.png"</xwikiAbsoluteUrl></attachment>');

This example is actually using a URL to a preexisting Attachment, just seeing if I'm getting permission errors on the server some how. I've also tried random publicly accessible images.

I get a 200 response signaling an attachment is created and a link appears on the page in the attachment section but the image won't display because 'it contains errors'.

Can I submit attachments this way? Do I need the actual image data, encoded somehow?
Reply | Threaded
Open this post in threaded view
|

Re: create attachment with Rest API

Fabio Mancinelli-4
On Wed, Feb 13, 2013 at 6:05 PM, zacharykane <[hidden email]> wrote:

> Hello, I'm trying to create an attachment on a page from a URL to an image,
> using JavaScript and the RESTful API.
>
> I'm not sure I'm doing it correctly of if this can be done but here's where
> I'm at:
>
> httpRequest.open('PUT', '/xwiki/rest/wikis/XWiki/spaces/Main/pages/Daily
> All-Source Digest – 2013-01-05T07:30P24/attachments/Blade Header
> UnClassified-S.png');
>

I think the following is wrong. You just have to send (PUT) the actual
PNG data using application/octet-stream media type and that's it. No
XML involved here.

> httpRequest.setRequestHeader('Content-Type', 'application/xml');
>
> httpRequest.send('<?xml version="1.0" encoding="UTF-8"
> standalone="yes"?><attachment xmlns="http://www.xwiki.org"><name>"Blade
> Header
> UnClassified-S.png"</name><mimeType>"image/png"</mimeType><xwikiAbsoluteUrl>"https://domain/xwiki/bin/download/ColorThemes/Nightfall/Blade%20Header%20UnClassified-S.png"</xwikiAbsoluteUrl></attachment>');
>
> This example is actually using a URL to a preexisting Attachment, just
> seeing if I'm getting permission errors on the server some how. I've also
> tried random publicly accessible images.
>
> I get a 200 response signaling an attachment is created and a link appears
> on the page in the attachment section but the image won't display because
> 'it contains errors'.
>
> Can I submit attachments this way? Do I need the actual image data, encoded
> somehow?
>

What you did, instead, is to send XML as PNG data, that's why your
file is seen as corrupted. You didn't send any actual PNG bytes.

Let me know if it works

-Fabio
_______________________________________________
devs mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/devs
Reply | Threaded
Open this post in threaded view
|

Re: create attachment with Rest API

zacharykane
Huh, well that's going to be much trickier then.

Would I set the RequestHeader, Content-Type to application/octet-stream?

So maybe this is beyond what can be answered here but getting the raw image data in JavaScript seems difficult, especially as I only have the URL to the image... base64 encoded data doesn't seem to count?
Reply | Threaded
Open this post in threaded view
|

Re: create attachment with Rest API

zacharykane
I think I should maybe put this another way.

What I'm trying to do is add an attachment to a page that is a remotely available image file. The way the process is desired to work is that the user doesn't have to download this image and then use the attachment form on the page manually. Rather, they'd like to know if it can be taken care of programmaticly.

I made an assumption of the REST API based upon the example of creating a Page. It seems that this won't be the same process with creating files - at least from a purely JavaScript perspective. Using Velocity can I achieve something like this server side?
Reply | Threaded
Open this post in threaded view
|

Re: create attachment with Rest API

Fabio Mancinelli-4
On Wed, Feb 13, 2013 at 8:15 PM, zacharykane <[hidden email]> wrote:

> I think I should maybe put this another way.
>
> What I'm trying to do is add an attachment to a page that is a remotely
> available image file. The way the process is desired to work is that the
> user doesn't have to download this image and then use the attachment form on
> the page manually. Rather, they'd like to know if it can be taken care of
> programmaticly.
>
> I made an assumption of the REST API based upon the example of creating a
> Page. It seems that this won't be the same process with creating files - at
> least from a purely JavaScript perspective. Using Velocity can I achieve
> something like this server side?
>
Sorry but I don't quite understand what you want to achieve.

If the image is available elsewhere you have two options:

1) Either you link it directly in the page using a URI (i.e., the URI
of the image)

2) If you want to have the same image also attached to the page (i.e.,
having a copy of it on the page) you still need to retrieve the actual
binary data of the image and then PUT it as the attachment to the page
as I told you earlier.

Creating an attachment that is a symbolic link to another file on the
web doesn't work because attachments are not post-processed, so an
attachment containing a URI-link to an image would be just be plain
string of character making up the URI, and not the referenced image.

Hope this helps.

-Fabio

>
>
> --
> View this message in context: http://xwiki.475771.n2.nabble.com/create-attachment-with-Rest-API-tp7583867p7583871.html
> Sent from the XWiki- Dev mailing list archive at Nabble.com.
> _______________________________________________
> devs mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/devs
_______________________________________________
devs mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/devs
Reply | Threaded
Open this post in threaded view
|

Re: create attachment with Rest API

jerem
In reply to this post by zacharykane
Hi,

If your assumption of using REST API is not really a need, then personnally
I would do things differently. I can just expose general idea, don't have
much time to go into details but maybe it can help you ...
- create a new specific page in XWiki (RemoteAttachmentService ?), that
will, using some parameters provided when targeting its url (url of remote
image, name of xwiki page to create attachment, filename of this
attachment), do the following:
   - perform a GET on that url to retrieve image bytes (I think you can
find Groovy examples by looking at extensions about Jira or Nexus
integration on xwiki.org for the GET), or to save that image as a temporary
file somewhere
   - use XWiki API to create or update a new page, create a new Attachment
on this page and set the image bytes as its content, save that doc
   - write some JSON as unique page content with info you like (result
success/failure, error message, whatever...)
- from the page where you want your users to add that sort of attachments,
perform an Ajax request on RemoteAttachmentService, with needed parameters,
and manage success/failure to display result to user

I assumed that you didn't want to reload the page (as you wanted to make it
in javascript), if you don't care you can use only what I called
RemoteAttachmentService as unique page, and don't bother with Ajax call, of
course.

BR,
Jeremie


2013/2/13 zacharykane <[hidden email]>

> I think I should maybe put this another way.
>
> What I'm trying to do is add an attachment to a page that is a remotely
> available image file. The way the process is desired to work is that the
> user doesn't have to download this image and then use the attachment form
> on
> the page manually. Rather, they'd like to know if it can be taken care of
> programmaticly.
>
> I made an assumption of the REST API based upon the example of creating a
> Page. It seems that this won't be the same process with creating files - at
> least from a purely JavaScript perspective. Using Velocity can I achieve
> something like this server side?
>
>
>
> --
> View this message in context:
> http://xwiki.475771.n2.nabble.com/create-attachment-with-Rest-API-tp7583867p7583871.html
> Sent from the XWiki- Dev mailing list archive at Nabble.com.
> _______________________________________________
> devs mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/devs
>
_______________________________________________
devs mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/devs