Skip to content

Commit 844d65f

Browse files
Fix assorted tests
1 parent 7146a99 commit 844d65f

File tree

10 files changed

+33
-64
lines changed

10 files changed

+33
-64
lines changed

api/src/org/labkey/api/reports/report/r/RReportJob.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
import java.io.File;
4444
import java.io.Serializable;
45+
import java.nio.file.CopyOption;
46+
import java.nio.file.StandardCopyOption;
4547
import java.util.ArrayList;
4648
import java.util.HashMap;
4749
import java.util.List;
@@ -315,7 +317,7 @@ protected void processOutputs(RReport report, List<ParamReplacement> outputSubst
315317
{
316318
newFile = FileUtil.createTempFile(LOG_FILE_PREFIX, ".log", parentDir);
317319
getJob().setLogFile(newFile);
318-
FileUtil.copyFile(file, newFile);
320+
FileUtil.copyFile(file, newFile, StandardCopyOption.REPLACE_EXISTING);
319321
}
320322
// report.log != getLogFile(), just regular file
321323
else

api/src/org/labkey/api/study/actions/TransformResultsAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public ModelAndView getView(TransformResultsForm form, BindException errors) thr
4343
{
4444
FileLike downloadFile = transformDir.resolveChild(form.getName());
4545
// isn't this always true?
46-
if(URIUtil.isDescendant(transformDir.toURI(), downloadFile.toURI()))
46+
if (URIUtil.isDescendant(transformDir.toURI(), downloadFile.toURI()))
4747
{
4848
HttpServletResponse response = getViewContext().getResponse();
49-
PageFlowUtil.streamFile(response, FileSystemLike.toFile(downloadFile), true);
49+
PageFlowUtil.streamFile(response, downloadFile, true);
5050
}
5151
}
5252

api/src/org/labkey/api/util/ImageUtil.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
import javax.imageio.ImageIO;
3939
import java.awt.image.BufferedImage;
40-
import java.io.File;
4140
import java.io.IOException;
41+
import java.io.InputStream;
4242
import java.io.OutputStream;
4343
import java.net.URI;
4444
import java.net.URISyntaxException;
@@ -258,10 +258,10 @@ public void setBaseURL(String url)
258258
* Retrieves a file cached in the session
259259
*/
260260
@Nullable
261-
public static File getFileFromSession(HttpServletRequest request, String key)
261+
public static FileLike getFileFromSession(HttpServletRequest request, String key)
262262
{
263263
Object o = request.getSession().getAttribute(key);
264-
if (o instanceof File file && file.exists())
264+
if (o instanceof FileLike file && file.exists())
265265
return file;
266266

267267
return null;
@@ -314,26 +314,22 @@ public ImageResource getImageResource(String uri)
314314
String sessionKey = helper.getParameter(FILE_SESSION_PARAM);
315315
String deleteFile = helper.getParameter(DELETE_FILE_PARAM);
316316

317-
File file = getFileFromSession(_context.getRequest(), sessionKey);
317+
FileLike file = getFileFromSession(_context.getRequest(), sessionKey);
318318
if (file != null)
319319
{
320-
try
320+
try (InputStream in = file.openInputStream())
321321
{
322-
BufferedImage img = ImageIO.read(file);
322+
BufferedImage img = ImageIO.read(in);
323323
ir = createImageResource(uri, img);
324324
_imageCache.put(uri, ir);
325325

326326
if (BooleanUtils.toBoolean(deleteFile))
327327
file.delete();
328328
}
329-
catch(IOException e)
330-
{
331-
}
329+
catch (IOException ignored) {}
332330
}
333331
}
334-
catch(URISyntaxException e)
335-
{
336-
}
332+
catch (URISyntaxException ignored) {}
337333
}
338334

339335
if (ir != null)

api/src/org/labkey/api/util/PageFlowUtil.java

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import org.labkey.api.view.template.ClientDependency;
8181
import org.labkey.api.view.template.PageConfig;
8282
import org.labkey.api.writer.ContainerUser;
83+
import org.labkey.vfs.FileLike;
8384
import org.springframework.beans.PropertyValue;
8485
import org.springframework.beans.PropertyValues;
8586
import org.springframework.web.util.WebUtils;
@@ -986,29 +987,6 @@ private static void _prepareResponseForFile(HttpServletResponse response, Map<St
986987
response.setHeader(entry.getKey(), entry.getValue());
987988
}
988989

989-
/**
990-
* Read the file and stream it to the browser through the response.
991-
*
992-
* @param detectContentType If set to true, then the content type is detected, else it is inferred from the extension
993-
* of the file name.
994-
*/
995-
public static void streamFile(HttpServletResponse response, File file, boolean asAttachment, boolean detectContentType) throws IOException
996-
{
997-
if (detectContentType)
998-
streamFile(response, Collections.emptyMap(), file, asAttachment);
999-
else
1000-
{
1001-
try
1002-
{
1003-
streamFile(response, Collections.emptyMap(), file.getName(), new FileInputStream(file), asAttachment);
1004-
}
1005-
catch (FileNotFoundException e)
1006-
{
1007-
throw new NotFoundException(file.getName());
1008-
}
1009-
}
1010-
}
1011-
1012990
public static void streamFile(HttpServletResponse response, Path file, boolean asAttachment, boolean detectContentType) throws IOException
1013991
{
1014992
String filename = file.getFileName().toString();
@@ -1027,26 +1005,19 @@ public static void streamFile(HttpServletResponse response, Path file, boolean a
10271005
}
10281006
}
10291007

1030-
@Deprecated // Prefer the Path version
1031-
public static void streamFile(HttpServletResponse response, File file, boolean asAttachment) throws IOException
1008+
public static void streamFile(HttpServletResponse response, FileLike file, boolean asAttachment) throws IOException
10321009
{
1033-
streamFile(response, file.toPath(), asAttachment, false);
1010+
streamFile(response, file.toNioPathForRead(), asAttachment, false);
10341011
}
10351012

10361013
public static void streamFile(HttpServletResponse response, Path file, boolean asAttachment) throws IOException
10371014
{
10381015
streamFile(response, file, asAttachment, false);
10391016
}
10401017

1041-
1042-
/**
1043-
* Read the file and stream it to the browser through the response. The content type of the file is detected
1044-
* from the contents of the file.
1045-
*/
1046-
@Deprecated //Prefer the Path version
1047-
public static void streamFile(@NotNull HttpServletResponse response, @NotNull Map<String, String> responseHeaders, File file, boolean asAttachment) throws IOException
1018+
public static void streamFile(@NotNull HttpServletResponse response, @NotNull Map<String, String> responseHeaders, FileLike file, boolean asAttachment) throws IOException
10481019
{
1049-
streamFile(response,responseHeaders, file.toPath(), asAttachment);
1020+
streamFile(response, responseHeaders, file.toNioPathForRead(), asAttachment);
10501021
}
10511022

10521023
public static void streamFile(@NotNull HttpServletResponse response, @NotNull Map<String, String> responseHeaders, Path file, boolean asAttachment) throws IOException

api/src/org/labkey/api/writer/ZipUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public static void zipToStream(HttpServletResponse response, File file, boolean
175175

176176
if (preZipped)
177177
{
178-
PageFlowUtil.streamFile(response, file, true);
178+
PageFlowUtil.streamFile(response, file.toPath(), true);
179179
return;
180180
}
181181

experiment/src/org/labkey/experiment/controllers/exp/ExperimentController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ public ModelAndView getView(ExperimentRunForm form, BindException errors) throws
18831883

18841884
try
18851885
{
1886-
PageFlowUtil.streamFile(getViewContext().getResponse(), new File(files.getImageFile().getAbsolutePath()), false);
1886+
PageFlowUtil.streamFile(getViewContext().getResponse(), files.getImageFile().toPath(), false);
18871887
}
18881888
catch (FileNotFoundException e)
18891889
{
@@ -6639,7 +6639,7 @@ public ModelAndView getView(ShowExternalDocsForm form, BindException errors) thr
66396639
throw new NotFoundException();
66406640
}
66416641

6642-
PageFlowUtil.streamFile(getViewContext().getResponse(), new File(f.getAbsolutePath()), false);
6642+
PageFlowUtil.streamFile(getViewContext().getResponse(), f.toPath(), false);
66436643
return null;
66446644
}
66456645

pipeline/src/org/labkey/pipeline/PipelineController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ public ModelAndView getView(PathForm form, BindException errors) throws Exceptio
11151115
if (!pipeRoot.hasPermission(getUser(), ReadPermission.class))
11161116
throw new UnauthorizedException();
11171117

1118-
File file = pipeRoot.resolvePath(form.getPath());
1118+
FileLike file = pipeRoot.resolvePathToFileLike(form.getPath());
11191119
if (!file.exists() || !file.isFile())
11201120
throw new NotFoundException();
11211121

pipeline/src/org/labkey/pipeline/api/ScriptTaskImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,10 @@ protected void writeTaskInfo(FileLike file, RecordedAction action) throws IOExce
310310
}
311311
}
312312

313-
try (TSVMapWriter tsvWriter = new TSVMapWriter(columns, rows);
314-
OutputStream out = file.openOutputStream())
313+
try (TSVMapWriter tsvWriter = new TSVMapWriter(columns, rows))
315314
{
316315
tsvWriter.setHeaderRowVisible(false);
317-
tsvWriter.write(out);
316+
tsvWriter.write(file.openOutputStream());
318317
}
319318
}
320319

query/src/org/labkey/query/reports/ReportsController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ public ModelAndView getView(Object o, BindException errors) throws Exception
15111511
String cacheFile = (String) getViewContext().get(ImageUtil.CACHE_FILE_PARAM);
15121512
if (sessionKey != null)
15131513
{
1514-
File file = ImageUtil.getFileFromSession(getViewContext().getRequest(), sessionKey);
1514+
FileLike file = ImageUtil.getFileFromSession(getViewContext().getRequest(), sessionKey);
15151515
if (file != null)
15161516
{
15171517
Map<String, String> responseHeaders = Collections.emptyMap();
@@ -1521,18 +1521,18 @@ public ModelAndView getView(Object o, BindException errors) throws Exception
15211521

15221522
responseHeaders.put("Pragma", "private");
15231523
responseHeaders.put("Cache-Control", "private, max-age=3600");
1524-
_log.debug("Caching file: " + file.getAbsolutePath());
1524+
_log.debug("Caching file: " + file);
15251525
}
1526-
PageFlowUtil.streamFile(getViewContext().getResponse(), responseHeaders, file.toPath(), BooleanUtils.toBoolean(attachment));
1526+
PageFlowUtil.streamFile(getViewContext().getResponse(), responseHeaders, file, BooleanUtils.toBoolean(attachment));
15271527
if (BooleanUtils.toBoolean(deleteFile))
15281528
{
15291529
file.delete();
1530-
_log.debug("Deleting file: " + file.getAbsolutePath());
1530+
_log.debug("Deleting file: " + file);
15311531
}
15321532
return null;
15331533
}
15341534
}
1535-
return new HtmlView(HtmlString.of("Requested Resource not found"));
1535+
return new HtmlView(HtmlString.of("Requested resource not found"));
15361536
}
15371537

15381538
@Override
@@ -2008,7 +2008,7 @@ public ModelAndView getView(ModuleReportForm form, BindException errors) throws
20082008
Resource imageResource = ReportUtil.getModuleImageFile(report, imageFilePrefix);
20092009

20102010
if (null != imageResource)
2011-
PageFlowUtil.streamFile(getViewContext().getResponse(), ((FileResource) imageResource).getFile(), true);
2011+
PageFlowUtil.streamFile(getViewContext().getResponse(), ((FileResource) imageResource).getFile().toPath(), true);
20122012

20132013
return null;
20142014
}

study/src/org/labkey/study/controllers/reports/ReportsController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import org.labkey.study.query.StudyQuerySchema;
8888
import org.labkey.study.reports.ParticipantReport;
8989
import org.labkey.study.reports.ReportManager;
90+
import org.labkey.vfs.FileLike;
9091
import org.springframework.validation.BindException;
9192
import org.springframework.validation.Errors;
9293
import org.springframework.web.servlet.ModelAndView;
@@ -149,10 +150,10 @@ public ModelAndView getView(Object o, BindException errors) throws Exception
149150
return null;
150151
}
151152

152-
File file = ImageUtil.getFileFromSession(getViewContext().getRequest(), sessionKey);
153+
FileLike file = ImageUtil.getFileFromSession(getViewContext().getRequest(), sessionKey);
153154
if (file != null)
154155
{
155-
PageFlowUtil.streamFile(getViewContext().getResponse(), file.toPath(), false);
156+
PageFlowUtil.streamFile(getViewContext().getResponse(), file, false);
156157
file.delete();
157158
}
158159
return null;

0 commit comments

Comments
 (0)