mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Partial fix for AWC-999: Content upload guesses character encoding and offers the user the chance to change it.
TODO: Fix the "Modify Content Properties" to include the encoding as a changeable option. Modified other entry points of content into the system. All calls to ContentWriter.setEncoding("UTF-8") need some serious examination. It is no longer necessary to assume anything about the encoding. The worst case scenario is that we guess the encoding from the stream without giving the user the chance to change it. This works for most non-interactive scenarios like CIFS, WebDAV and FTP, now. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6113 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -24,7 +24,10 @@
|
||||
*/
|
||||
package org.alfresco.web.bean.content;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
@@ -68,6 +71,7 @@ public class AddContentDialog extends BaseContentWizard
|
||||
// Try and extract metadata from the file
|
||||
ContentReader cr = new FileContentReader(this.file);
|
||||
cr.setMimetype(this.mimeType);
|
||||
cr.setEncoding(this.encoding);
|
||||
// create properties for content type
|
||||
Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>(5, 1.0f);
|
||||
|
||||
@@ -148,9 +152,29 @@ public class AddContentDialog extends BaseContentWizard
|
||||
// NOTE: This is a far from ideal solution but will do until we have
|
||||
// a pure JSF upload solution working. This method is only called
|
||||
// after a file is uploaded, so we can calculate the mime type and
|
||||
// determine whether to enable inline editing in here.
|
||||
this.mimeType = Repository.getMimeTypeForFileName(
|
||||
FacesContext.getCurrentInstance(), this.fileName);
|
||||
// determine whether to enable inline editing in here.
|
||||
FacesContext fc = FacesContext.getCurrentInstance();
|
||||
this.mimeType = Repository.getMimeTypeForFileName(fc, this.fileName);
|
||||
this.encoding = "UTF-8";
|
||||
InputStream is = null;
|
||||
try
|
||||
{
|
||||
if (this.file != null)
|
||||
{
|
||||
is = new BufferedInputStream(new FileInputStream(this.file));
|
||||
this.encoding = Repository.guessEncoding(fc, is, this.mimeType);
|
||||
}
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
// Not terminal
|
||||
logger.error("Failed to get encoding from file: " + this.fileName, e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { is.close(); } catch (Throwable e) {} // Includes NPE
|
||||
}
|
||||
|
||||
this.inlineEdit = (this.mimeType.equals(MimetypeMap.MIMETYPE_HTML));
|
||||
|
||||
// get the file upload message
|
||||
|
@@ -26,6 +26,7 @@ package org.alfresco.web.bean.content;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -68,6 +69,7 @@ public abstract class BaseContentWizard extends BaseWizardBean
|
||||
protected String title;
|
||||
protected String description;
|
||||
protected String mimeType;
|
||||
protected String encoding;
|
||||
protected String objectType;
|
||||
protected boolean inlineEdit;
|
||||
protected boolean otherPropertiesChoiceVisible = true;
|
||||
@@ -159,7 +161,23 @@ public abstract class BaseContentWizard extends BaseWizardBean
|
||||
{
|
||||
this.mimeType = mimeType;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Returns the encoding currently selected
|
||||
*/
|
||||
public String getEncoding()
|
||||
{
|
||||
return encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param encoding the document's encoding
|
||||
*/
|
||||
public void setEncoding(String encoding)
|
||||
{
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the object type currenty selected
|
||||
*/
|
||||
@@ -250,6 +268,18 @@ public abstract class BaseContentWizard extends BaseWizardBean
|
||||
this.showOtherProperties = showOthers;
|
||||
}
|
||||
|
||||
public List<SelectItem> getEncodings()
|
||||
{
|
||||
Map<String, Charset> availableCharsets = Charset.availableCharsets();
|
||||
List<SelectItem> items = new ArrayList<SelectItem>(availableCharsets.size());
|
||||
for (Charset charset : availableCharsets.values())
|
||||
{
|
||||
SelectItem item = new SelectItem(charset.name(), charset.displayName());
|
||||
items.add(item);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns a list of object types to allow the user to select from
|
||||
*/
|
||||
@@ -407,7 +437,7 @@ public abstract class BaseContentWizard extends BaseWizardBean
|
||||
ContentWriter writer = contentService.getWriter(fileNodeRef, ContentModel.PROP_CONTENT, true);
|
||||
// set the mimetype and encoding
|
||||
writer.setMimetype(this.mimeType);
|
||||
writer.setEncoding("UTF-8");
|
||||
writer.setEncoding(this.encoding);
|
||||
if (fileContent != null)
|
||||
{
|
||||
writer.putContent(fileContent);
|
||||
|
Reference in New Issue
Block a user