jejacks0n/mercury — Issue #43: Allow for uploading of specific files besides images
Author: JeanMertz
Created: 2011-10-19 13:50:29+00:00
State: open

I didn't want to make a pull request out of this yet, but I hacked the current uploader to allow for documents to be uploaded:

Basically it extends the current functionality by checking if the uploaded file is an image, and if it isn't, use the new code. The server has to give back an extra value, called "response.document.img". When the user has text selected and uploads a document, the selected text is converted to a link, if there is an image in the selection, that image is wrapped in a link to the document. If there is no selection, a new image is used with the `src` set to `response.document.img`, on the server side I then check for the mime type and pass back the appropriate image url, for example a small PDF image, or Word image.

This needs a lot of work, and it would be best to extract this out into a separate image uploader and document uploader (because the image previewing feature (during uploading) now shows a broken image), but I don't think I am comfortable with the code yet to do this and I definitely don't have the time for the next few weeks.

Anyway, here is the snippet:

```CoffeeScript
if ['image/jpeg', 'image/gif', 'image/png'].indexOf(@file.type) > -1
Mercury.trigger('action', {action: 'insertImage', value: {src: response.document.url}})
else
attrs = {href: response.document.url}
content = jQuery('', {src: response.document.img})
selection = Mercury.region.selection()

if selection.range.collapsed is false
if selection.commonAncestor && selection.commonAncestor(true).find('img').length > 0
content = selection.commonAncestor(true).find('img')[0]
else
content = selection.fragment.textContent

container = selection.commonAncestor(true).closest('a') if selection && selection.commonAncestor

if container && container.length
Mercury.trigger('action', {action: 'replaceLink', value: {tagName: 'a', attrs: attrs, content: content}, node: container.get(0)})
else
Mercury.trigger('action', {action: 'insertLink', value: {tagName: 'a', attrs: attrs, content: content}})
```