Search in sources :

Example 6 with ImageInfo

use of org.mifos.framework.image.domain.ImageInfo in project head by mifos.

the class ImageStorageManager method createImage.

public static ImageInfo createImage(InputStream in, String name) throws IOException {
    if (in == null) {
        return null;
    }
    String contentType;
    contentType = URLConnection.guessContentTypeFromStream(in);
    if (contentType == null) {
        contentType = DEFAULT_CONTENT_TYPE;
    }
    byte[] data = InputStreamUtils.getBytes(in);
    if (data.length == 0) {
        return null;
    }
    String filePath = generateRandomSubDir() + name;
    File file = new File(getStorageLocation() + filePath);
    FileUtils.touch(file);
    FileUtils.writeByteArrayToFile(file, data);
    ImageInfo imInfo = new ImageInfo();
    imInfo.setContentType(contentType);
    imInfo.setLength(Long.valueOf(data.length));
    imInfo.setPath(filePath);
    return imInfo;
}
Also used : ImageInfo(org.mifos.framework.image.domain.ImageInfo) File(java.io.File)

Example 7 with ImageInfo

use of org.mifos.framework.image.domain.ImageInfo in project head by mifos.

the class ClientPhotoServiceDatabaseIntegrationTest method TestCRUD.

@Test
public void TestCRUD() {
    final Long clientId = (long) this.testClient.getCustomerId().intValue();
    /* Create */
    final String data = "test string";
    InputStream in = new ByteArrayInputStream(data.getBytes());
    this.clientPhotoService.create(clientId, in);
    ClientPhoto cp = this.clientPhotoService.read(clientId);
    Assert.assertEquals(clientId, cp.getClientId());
    ImageInfo imageInfo = cp.getImageInfo();
    String path = imageInfo.getPath();
    Assert.assertNotNull(imageInfo.getCustomerPictureEntity().getPicture());
    Assert.assertNotNull(imageInfo.getContentType());
    Assert.assertEquals(data.length(), imageInfo.getLength().intValue());
    Assert.assertEquals(data, new String(this.clientPhotoService.getData(cp)));
    /* Update */
    final String otherData = "other test string";
    in = new ByteArrayInputStream(otherData.getBytes());
    this.clientPhotoService.update(clientId, in);
    cp = this.clientPhotoService.read(clientId);
    Assert.assertEquals(path, imageInfo.getPath());
    Assert.assertNotNull(imageInfo.getContentType());
    Assert.assertEquals(otherData.length(), imageInfo.getLength().intValue());
    Assert.assertEquals(otherData, new String(this.clientPhotoService.getData(cp)));
    Assert.assertTrue(this.clientPhotoService.delete(clientId));
    /* Delete */
    this.clientPhotoService.delete(clientId);
    cp = this.clientPhotoService.read(clientId);
    Assert.assertNull(cp);
}
Also used : ClientPhoto(org.mifos.framework.image.domain.ClientPhoto) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInfo(org.mifos.framework.image.domain.ImageInfo) Test(org.junit.Test)

Example 8 with ImageInfo

use of org.mifos.framework.image.domain.ImageInfo in project head by mifos.

the class ClientPhotoServiceIntegrationTest method testCRUD.

@Test
public void testCRUD() {
    String data = "test string";
    InputStream in = new ByteArrayInputStream(data.getBytes());
    Long clientId = 2342L;
    clientPhotoService.create(clientId, in);
    ClientPhoto cp = clientPhotoService.read(2342L);
    Assert.assertEquals(clientId, cp.getClientId());
    ImageInfo imageInfo = cp.getImageInfo();
    String path = imageInfo.getPath();
    Assert.assertNotNull(path);
    Assert.assertNotNull(imageInfo.getContentType());
    Assert.assertEquals(data.length(), imageInfo.getLength().intValue());
    Assert.assertEquals(data, new String(clientPhotoService.getData(cp)));
    String otherData = "other test string";
    in = new ByteArrayInputStream(otherData.getBytes());
    clientPhotoService.update(clientId, in);
    Assert.assertEquals(path, imageInfo.getPath());
    Assert.assertNotNull(imageInfo.getContentType());
    Assert.assertEquals(otherData.length(), imageInfo.getLength().intValue());
    Assert.assertEquals(otherData, new String(clientPhotoService.getData(cp)));
    Assert.assertTrue(clientPhotoService.delete(clientId));
}
Also used : ClientPhoto(org.mifos.framework.image.domain.ClientPhoto) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInfo(org.mifos.framework.image.domain.ImageInfo) Test(org.junit.Test)

Aggregations

ImageInfo (org.mifos.framework.image.domain.ImageInfo)8 ClientPhoto (org.mifos.framework.image.domain.ClientPhoto)7 IOException (java.io.IOException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Blob (java.sql.Blob)2 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 BusinessRuleException (org.mifos.service.BusinessRuleException)2 File (java.io.File)1 CustomerPictureEntity (org.mifos.customers.client.business.CustomerPictureEntity)1