parent
7b41bbe0b0
commit
3a7812aef7
@ -30,12 +30,10 @@
|
|||||||
|
|
||||||
package com.google.refine.extension.gdata;
|
package com.google.refine.extension.gdata;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.net.URL;
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
@ -44,7 +42,7 @@ import javax.servlet.ServletException;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -69,7 +67,7 @@ public class UploadCommand extends Command {
|
|||||||
static final Logger logger = LoggerFactory.getLogger("gdata_upload");
|
static final Logger logger = LoggerFactory.getLogger("gdata_upload");
|
||||||
|
|
||||||
private static final String METADATA_DESCRIPTION = "OpenRefine project dump";
|
private static final String METADATA_DESCRIPTION = "OpenRefine project dump";
|
||||||
private static final String METADATA_ICONLINK = "https://raw.githubusercontent.com/OpenRefine/OpenRefine/master/main/webapp/modules/core/images/logo-openrefine-550.png";
|
private static final String METADATA_ICON_FILE = "logo-openrefine-550.png";
|
||||||
@Override
|
@Override
|
||||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws ServletException, IOException {
|
throws ServletException, IOException {
|
||||||
@ -132,7 +130,7 @@ public class UploadCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static private String upload(
|
private String upload(
|
||||||
Project project, Engine engine, Properties params,
|
Project project, Engine engine, Properties params,
|
||||||
String token, String name, List<Exception> exceptions) {
|
String token, String name, List<Exception> exceptions) {
|
||||||
String format = params.getProperty("format");
|
String format = params.getProperty("format");
|
||||||
@ -144,22 +142,12 @@ public class UploadCommand extends Command {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] getImageFromUrl(String urlText) throws IOException {
|
protected byte[] getIconImage() throws IOException {
|
||||||
URL url = new URL(urlText);
|
InputStream is = getClass().getResourceAsStream(METADATA_ICON_FILE);
|
||||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
return IOUtils.toByteArray(is);
|
||||||
|
|
||||||
try (InputStream inputStream = url.openStream()) {
|
|
||||||
int n = 0;
|
|
||||||
byte [] buffer = new byte[ 1024 ];
|
|
||||||
while (-1 != (n = inputStream.read(buffer))) {
|
|
||||||
output.write(buffer, 0, n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return output.toByteArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String uploadOpenRefineProject(Project project, String token,
|
private String uploadOpenRefineProject(Project project, String token,
|
||||||
String name, List<Exception> exceptions) {
|
String name, List<Exception> exceptions) {
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
|
|
||||||
@ -170,14 +158,12 @@ public class UploadCommand extends Command {
|
|||||||
fos = new FileOutputStream(filePath);
|
fos = new FileOutputStream(filePath);
|
||||||
FileProjectManager.gzipTarToOutputStream(project, fos);
|
FileProjectManager.gzipTarToOutputStream(project, fos);
|
||||||
|
|
||||||
File fileMetadata = new File();
|
|
||||||
String asB64 = Base64.encodeBase64URLSafeString(getImageFromUrl(METADATA_ICONLINK));
|
|
||||||
|
|
||||||
Thumbnail tn = new Thumbnail();
|
Thumbnail tn = new Thumbnail();
|
||||||
tn.setMimeType("image/x-icon").setImage(asB64);
|
tn.setMimeType("image/x-icon").encodeImage(getIconImage());
|
||||||
ContentHints contentHints = new ContentHints();
|
ContentHints contentHints = new ContentHints();
|
||||||
contentHints.setThumbnail(tn);
|
contentHints.setThumbnail(tn);
|
||||||
|
|
||||||
|
File fileMetadata = new File();
|
||||||
fileMetadata.setName(name)
|
fileMetadata.setName(name)
|
||||||
.setDescription(METADATA_DESCRIPTION)
|
.setDescription(METADATA_DESCRIPTION)
|
||||||
.setContentHints(contentHints);
|
.setContentHints(contentHints);
|
||||||
|
Binary file not shown.
After Width: | Height: | Size: 58 KiB |
@ -2,6 +2,7 @@ package com.google.refine.extension.gdata;
|
|||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
@ -19,12 +20,21 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
|||||||
import com.google.refine.commands.Command;
|
import com.google.refine.commands.Command;
|
||||||
import com.google.refine.util.ParsingUtilities;
|
import com.google.refine.util.ParsingUtilities;
|
||||||
|
|
||||||
|
|
||||||
|
class UploadCommandStub extends UploadCommand {
|
||||||
|
|
||||||
|
public byte[] getIcon() throws IOException {
|
||||||
|
return getIconImage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class UploadCommandTest {
|
public class UploadCommandTest {
|
||||||
protected HttpServletRequest request = null;
|
protected HttpServletRequest request = null;
|
||||||
protected HttpServletResponse response = null;
|
protected HttpServletResponse response = null;
|
||||||
protected Command command = null;
|
protected Command command = null;
|
||||||
protected StringWriter writer = null;
|
protected StringWriter writer = null;
|
||||||
|
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
public void setUpRequestResponse() {
|
public void setUpRequestResponse() {
|
||||||
request = mock(HttpServletRequest.class);
|
request = mock(HttpServletRequest.class);
|
||||||
@ -46,4 +56,10 @@ public class UploadCommandTest {
|
|||||||
ParsingUtilities.mapper.readValue(writer.toString(), ObjectNode.class));
|
ParsingUtilities.mapper.readValue(writer.toString(), ObjectNode.class));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIconRead() throws IOException {
|
||||||
|
UploadCommandStub cmd = new UploadCommandStub();
|
||||||
|
assertEquals(cmd.getIconImage().length, 58994);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user