use of org.sagebionetworks.repo.model.attachment.AttachmentData in project Synapse-Repository-Services by Sage-Bionetworks.
the class StorageLocationUtilsTest method before.
@Before
public void before() {
Long nodeId = Long.valueOf(111L);
Long userId = Long.valueOf(333L);
List<AttachmentData> attachmentList = new ArrayList<AttachmentData>();
AttachmentData ad = new AttachmentData();
ad.setName("ad1");
ad.setTokenId("ad1Token");
ad.setContentType("ad1Code");
ad.setMd5("ad1Md5");
ad.setUrl("ad1Url");
ad.setPreviewId("ad1Preview");
attachmentList.add(ad);
ad = new AttachmentData();
ad.setName("ad2");
ad.setTokenId("ad2Token");
ad.setContentType("ad2Code");
ad.setMd5("ad2Md5");
ad.setUrl("ad2Url");
ad.setPreviewId("ad2Preview");
attachmentList.add(ad);
List<LocationData> locationList = new ArrayList<LocationData>();
LocationData ld = new LocationData();
ld.setPath("/ld1Path");
ld.setType(LocationTypeNames.external);
locationList.add(ld);
ld = new LocationData();
ld.setPath("/abc/xyz");
ld.setType(LocationTypeNames.awss3);
locationList.add(ld);
Map<String, List<String>> strAnnotations = new HashMap<String, List<String>>();
List<String> md5List = new ArrayList<String>();
md5List.add("ldMd5");
strAnnotations.put("md5", md5List);
List<String> ctList = new ArrayList<String>();
ctList.add("ldContentType");
strAnnotations.put("contentType", ctList);
locations = new StorageLocations(nodeId, userId, attachmentList, locationList, strAnnotations);
List<S3ObjectSummary> objList1 = new ArrayList<S3ObjectSummary>();
S3ObjectSummary objSummary = mock(S3ObjectSummary.class);
when(objSummary.getKey()).thenReturn("111/ad1Token");
when(objSummary.getSize()).thenReturn(12345L);
objList1.add(objSummary);
ObjectListing objListing1 = mock(ObjectListing.class);
when(objListing1.getObjectSummaries()).thenReturn(objList1);
when(objListing1.isTruncated()).thenReturn(true);
List<S3ObjectSummary> objList2 = new ArrayList<S3ObjectSummary>();
objSummary = mock(S3ObjectSummary.class);
when(objSummary.getKey()).thenReturn("abc/xyz");
when(objSummary.getSize()).thenReturn(54321L);
objList2.add(objSummary);
objSummary = mock(S3ObjectSummary.class);
when(objSummary.getKey()).thenReturn("ld1Path");
when(objSummary.getSize()).thenReturn(99999L);
objList2.add(objSummary);
ObjectListing objListing2 = mock(ObjectListing.class);
when(objListing2.getObjectSummaries()).thenReturn(objList2);
when(objListing2.isTruncated()).thenReturn(false);
s3Client = mock(AmazonS3Client.class);
String bucket = StackConfiguration.getS3Bucket();
when(s3Client.listObjects(bucket, "111/")).thenReturn(objListing1);
when(s3Client.listNextBatchOfObjects(objListing1)).thenReturn(objListing2);
}
use of org.sagebionetworks.repo.model.attachment.AttachmentData in project Synapse-Repository-Services by Sage-Bionetworks.
the class Synapse method uploadAttachmentToSynapse.
/**
* Upload an attachment to Synapse.
* @param attachmentType
* @param userId
* @param dataFile
* @param md5
* @return
* @throws JSONObjectAdapterException
* @throws SynapseException
* @throws IOException
*/
public AttachmentData uploadAttachmentToSynapse(String id, AttachmentType attachmentType, File dataFile, String fileName) throws JSONObjectAdapterException, SynapseException, IOException {
// First we need to get an S3 token
S3AttachmentToken token = new S3AttachmentToken();
token.setFileName(fileName);
String md5 = MD5ChecksumHelper.getMD5Checksum(dataFile.getAbsolutePath());
token.setMd5(md5);
// Create the token
token = createAttachmentS3Token(id, attachmentType, token);
// Upload the file
dataUploader.uploadDataSingle(token, dataFile);
// We are now done
AttachmentData newData = new AttachmentData();
newData.setContentType(token.getContentType());
newData.setName(fileName);
newData.setTokenId(token.getTokenId());
newData.setMd5(token.getMd5());
return newData;
}
use of org.sagebionetworks.repo.model.attachment.AttachmentData in project Synapse-Repository-Services by Sage-Bionetworks.
the class IT500SynapseJavaClient method testAttachmentsImageRoundTrip.
/**
* Test that we can add an attachment to a project and then get it back.
*
* @throws IOException
* @throws JSONObjectAdapterException
* @throws SynapseException
*/
@Test
public void testAttachmentsImageRoundTrip() throws IOException, JSONObjectAdapterException, SynapseException {
// First load an image from the classpath
String fileName = "images/IMAG0019.jpg";
URL url = IT500SynapseJavaClient.class.getClassLoader().getResource(fileName);
assertNotNull("Failed to find: " + fileName + " on the classpath", url);
File originalFile = new File(url.getFile());
File attachmentDownload = File.createTempFile("AttachmentTestDownload", ".tmp");
File previewDownload = File.createTempFile("AttachmentPreviewDownload", ".png");
FileOutputStream writer = null;
FileInputStream reader = null;
try {
// We are now ready to add this file as an attachment on the project
String finalName = "iamgeFile.jpg";
AttachmentData data = synapse.uploadAttachmentToSynapse(project.getId(), originalFile, finalName);
assertEquals(finalName, data.getName());
// Save this this attachment on the entity.
project.setAttachments(new ArrayList<AttachmentData>());
project.getAttachments().add(data);
// Save this attachment to the project
project = synapse.putEntity(project);
assertNotNull(project.getAttachments());
assertEquals(1, project.getAttachments().size());
AttachmentData clone = project.getAttachments().get(0);
assertEquals(data.getName(), clone.getName());
assertEquals(data.getMd5(), clone.getMd5());
assertEquals(data.getContentType(), clone.getContentType());
assertEquals(data.getTokenId(), clone.getTokenId());
// the attachment should have preview
assertNotNull(clone.getPreviewId());
// Now make sure we can download our
synapse.downloadEntityAttachment(project.getId(), clone, attachmentDownload);
assertTrue(attachmentDownload.exists());
System.out.println(attachmentDownload.getAbsolutePath());
assertEquals(originalFile.length(), attachmentDownload.length());
// Now make sure we can get the preview image
// Before we download the preview make sure it exists
synapse.waitForPreviewToBeCreated(project.getId(), clone.getPreviewId(), PREVIEW_TIMOUT);
synapse.downloadEntityAttachmentPreview(project.getId(), clone.getPreviewId(), previewDownload);
assertTrue(previewDownload.exists());
System.out.println(previewDownload.getAbsolutePath());
assertTrue(previewDownload.length() > 0);
assertTrue("A preview size should not exceed 100KB. This one is " + previewDownload.length(), previewDownload.length() < 100 * 1000);
} finally {
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
attachmentDownload.delete();
previewDownload.delete();
}
}
use of org.sagebionetworks.repo.model.attachment.AttachmentData in project Synapse-Repository-Services by Sage-Bionetworks.
the class AttachmentManagerImplTest method testCreatePreviewImageLocalUrl.
@Test
public void testCreatePreviewImageLocalUrl() throws NotFoundException, DatastoreException, UnauthorizedException, InvalidModelException, InterruptedException {
AttachmentData data = new AttachmentData();
data.setName("shortWide.gif");
String fileName = "images/shortWide.gif";
URL url = AttachmentManagerUnitTest.class.getClassLoader().getResource(fileName);
assertNotNull("Could not find: " + fileName + " on the classpath", url);
data.setUrl(url.toString());
String entityId = "123";
ExampleEntity entity = new ExampleEntity();
entity.setId(entityId);
entity.setAttachments(new ArrayList<AttachmentData>());
entity.getAttachments().add(data);
// Make sure we can create a preview
attachmentManager.checkAttachmentsForPreviews(entity);
// Validate that an image was upload to S3.
assertEquals(PreviewState.PREVIEW_EXISTS, data.getPreviewState());
assertNotNull(data.getPreviewId());
// Make sure we can download the preview
String key = S3TokenManagerImpl.createAttachmentPathNoSlash(entity.getId(), data.getPreviewId());
waitForPreview(key);
// Make sure this preview gets deleted
keysToDelete.add(key);
// Wait for the key file to be finsihed
File temp = s3Utility.downloadFromS3(key);
assertNotNull(temp);
System.out.println(temp.getAbsolutePath());
assertTrue("The preview image should be larger than zero bytes", temp.length() > 0);
assertTrue("The preview image should be smaller than 100K bytes", temp.length() < 100 * 1000);
temp.delete();
}
use of org.sagebionetworks.repo.model.attachment.AttachmentData in project Synapse-Repository-Services by Sage-Bionetworks.
the class AttachmentManagerImplTest method testCreatePreviewImageS3.
@Ignore
@Test
public void testCreatePreviewImageS3() throws NotFoundException, DatastoreException, UnauthorizedException, InvalidModelException, InterruptedException {
String entityId = idGenerator.generateNewId().toString();
AttachmentData data = new AttachmentData();
data.setName("shortWide.gif");
String fileName = "images/shortWide.gif";
URL url = AttachmentManagerUnitTest.class.getClassLoader().getResource(fileName);
assertNotNull("Could not find: " + fileName + " on the classpath", url);
File toUpload = new File(url.getFile());
String tokenId = S3TokenManagerImpl.createTokenId(idGenerator.generateNewId(), data.getName());
String attachmentS3Key = S3TokenManagerImpl.createAttachmentPathNoSlash(entityId, tokenId);
// Upload this file
s3Utility.uploadToS3(toUpload, attachmentS3Key);
// Make sure this file gets deleted
keysToDelete.add(attachmentS3Key);
// Now create the preview
data.setTokenId(tokenId);
ExampleEntity entity = new ExampleEntity();
entity.setId(entityId);
entity.setAttachments(new ArrayList<AttachmentData>());
entity.getAttachments().add(data);
// Make sure we can create a preview
attachmentManager.checkAttachmentsForPreviews(entity);
// Validate that an image was upload to S3.
assertEquals(PreviewState.PREVIEW_EXISTS, data.getPreviewState());
assertNotNull(data.getPreviewId());
// Make sure we can download the preview
String previewKey = S3TokenManagerImpl.createAttachmentPathNoSlash(entity.getId(), data.getPreviewId());
// Wait for the preview
waitForPreview(previewKey);
// Make sure this preview gets deleted
keysToDelete.add(previewKey);
File temp = s3Utility.downloadFromS3(previewKey);
assertNotNull(temp);
System.out.println(temp.getAbsolutePath());
assertTrue("The preview image should be larger than zero bytes", temp.length() > 0);
assertTrue("The preview image should be smaller than 100K bytes", temp.length() < 100 * 1000);
temp.delete();
}
Aggregations