Skip to content

Commit 5f99e49

Browse files
committed
Added download method to ImageWrapper
1 parent f0008ae commit 5f99e49

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/main/java/fr/igred/omero/repository/ImageWrapper.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import omero.gateway.exception.DSAccessException;
4040
import omero.gateway.exception.DSOutOfServiceException;
4141
import omero.gateway.facility.ROIFacility;
42+
import omero.gateway.facility.TransferFacility;
4243
import omero.gateway.model.ChannelData;
4344
import omero.gateway.model.FolderData;
4445
import omero.gateway.model.ImageData;
@@ -52,6 +53,7 @@
5253
import java.awt.Color;
5354
import java.awt.image.BufferedImage;
5455
import java.io.ByteArrayInputStream;
56+
import java.io.File;
5557
import java.io.IOException;
5658
import java.sql.Timestamp;
5759
import java.util.ArrayList;
@@ -62,8 +64,7 @@
6264
import java.util.logging.Level;
6365
import java.util.logging.Logger;
6466

65-
import static fr.igred.omero.exception.ExceptionHandler.handleServiceOrAccess;
66-
import static fr.igred.omero.exception.ExceptionHandler.handleServiceOrServer;
67+
import static fr.igred.omero.exception.ExceptionHandler.*;
6768
import static omero.rtypes.rint;
6869

6970

@@ -572,4 +573,28 @@ public BufferedImage getThumbnail(Client client, int size) throws ServiceExcepti
572573
return thumbnail;
573574
}
574575

576+
577+
/**
578+
* Downloads the original files from the server.
579+
*
580+
* @param client The client handling the connection.
581+
* @param path Path to the file.
582+
*
583+
* @return See above.
584+
*
585+
* @throws OMEROServerError Server error.
586+
* @throws ServiceException Cannot connect to OMERO.
587+
* @throws AccessException Cannot access data.
588+
*/
589+
public List<File> download(Client client, String path)
590+
throws OMEROServerError, ServiceException, AccessException {
591+
try {
592+
TransferFacility transfer = client.getGateway().getFacility(TransferFacility.class);
593+
return transfer.downloadImage(client.getCtx(), path, getId());
594+
} catch (DSAccessException | DSOutOfServiceException | ExecutionException e) {
595+
handleException(e, "Could not download image " + getId() + ": " + e.getMessage());
596+
}
597+
return new ArrayList<>();
598+
}
599+
575600
}

src/test/java/fr/igred/omero/repository/ImageTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.io.FileOutputStream;
3939
import java.io.PrintStream;
4040
import java.nio.charset.StandardCharsets;
41+
import java.nio.file.Files;
4142
import java.text.SimpleDateFormat;
4243
import java.util.ArrayList;
4344
import java.util.Date;
@@ -678,4 +679,15 @@ public void testGetThumbnail() throws Exception {
678679
}
679680

680681

682+
@Test
683+
public void testDownload() throws Exception {
684+
ImageWrapper image = client.getImage(1L);
685+
List<File> files = image.download(client, ".");
686+
assertEquals(2, files.size());
687+
assertTrue(files.get(0).exists());
688+
Files.deleteIfExists(files.get(0).toPath());
689+
Files.deleteIfExists(files.get(1).toPath());
690+
}
691+
692+
681693
}

0 commit comments

Comments
 (0)