mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
MNT-21250: The fix for MNT-20734 introduces a problem whereby files larger than 4MB cannot be downloaded (#516)
- fixed temp file handling issue
This commit is contained in:
@@ -262,8 +262,15 @@ public class BufferedResponse implements WrappingWebScriptResponse
|
|||||||
if (logger.isDebugEnabled())
|
if (logger.isDebugEnabled())
|
||||||
logger.debug("Writing Transactional response: size=" + outputStream.getLength());
|
logger.debug("Writing Transactional response: size=" + outputStream.getLength());
|
||||||
|
|
||||||
outputStream.flush();
|
try
|
||||||
FileCopyUtils.copy(outputStream.getInputStream(), res.getOutputStream());
|
{
|
||||||
|
outputStream.flush();
|
||||||
|
FileCopyUtils.copy(outputStream.getInputStream(), res.getOutputStream());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
outputStream.destroy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
|
@@ -57,7 +57,6 @@ public abstract class ApiWebScript extends AbstractWebScript
|
|||||||
protected int memoryThreshold = 4 * 1024 * 1024; // 4mb
|
protected int memoryThreshold = 4 * 1024 * 1024; // 4mb
|
||||||
protected long maxContentSize = (long) 4 * 1024 * 1024 * 1024; // 4gb
|
protected long maxContentSize = (long) 4 * 1024 * 1024 * 1024; // 4gb
|
||||||
protected TempOutputStreamFactory streamFactory = null;
|
protected TempOutputStreamFactory streamFactory = null;
|
||||||
protected TempOutputStreamFactory responseStreamFactory = null;
|
|
||||||
protected TransactionService transactionService;
|
protected TransactionService transactionService;
|
||||||
|
|
||||||
public void setTransactionService(TransactionService transactionService)
|
public void setTransactionService(TransactionService transactionService)
|
||||||
@@ -98,7 +97,6 @@ public abstract class ApiWebScript extends AbstractWebScript
|
|||||||
{
|
{
|
||||||
File tempDirectory = TempFileProvider.getTempDir(tempDirectoryName);
|
File tempDirectory = TempFileProvider.getTempDir(tempDirectoryName);
|
||||||
this.streamFactory = new TempOutputStreamFactory(tempDirectory, memoryThreshold, maxContentSize, false, false);
|
this.streamFactory = new TempOutputStreamFactory(tempDirectory, memoryThreshold, maxContentSize, false, false);
|
||||||
this.responseStreamFactory = new TempOutputStreamFactory(tempDirectory, memoryThreshold, maxContentSize, false, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -2856,6 +2856,12 @@ public class NodeApiTest extends AbstractSingleNetworkSiteTest
|
|||||||
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
|
documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
|
||||||
|
|
||||||
assertTrue("File size is 0 bytes", documentResp.getContent().getSizeInBytes().intValue() > 0);
|
assertTrue("File size is 0 bytes", documentResp.getContent().getSizeInBytes().intValue() > 0);
|
||||||
|
|
||||||
|
// Download text content - by default with Content-Disposition header
|
||||||
|
response = getSingle(NodesEntityResource.class, docId + "/content", null, 200);
|
||||||
|
|
||||||
|
byte[] bytes = response.getResponseAsBytes();
|
||||||
|
assertEquals(contentSize.intValue(), bytes.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class UpdateNodeRunnable implements Runnable
|
private class UpdateNodeRunnable implements Runnable
|
||||||
@@ -3705,6 +3711,57 @@ public class NodeApiTest extends AbstractSingleNetworkSiteTest
|
|||||||
getSingle(getNodeContentUrl(contentNodeId), null, null, headers, 304);
|
getSingle(getNodeContentUrl(contentNodeId), null, null, headers, 304);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests download of file/content using backed temp file for streaming.
|
||||||
|
* <p>
|
||||||
|
* GET:
|
||||||
|
* </p>
|
||||||
|
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testDownloadFileContentUsingTempFile() throws Exception
|
||||||
|
{
|
||||||
|
setRequestContext(user1);
|
||||||
|
|
||||||
|
// This should be grater then TempOutputStream.DEFAULT_MEMORY_THRESHOLD
|
||||||
|
Long contentSize = 5 * 1024 * 1024L;
|
||||||
|
String fileName = "tempFile.txt";
|
||||||
|
|
||||||
|
File file = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
file = TempFileProvider.createTempFile(getClass().getSimpleName(), ".txt");
|
||||||
|
RandomAccessFile rndFile = new RandomAccessFile(file.getPath(), "rw");
|
||||||
|
rndFile.setLength(contentSize);
|
||||||
|
rndFile.close();
|
||||||
|
|
||||||
|
MultiPartBuilder multiPartBuilder = MultiPartBuilder.create().setFileData(new FileData(fileName, file));
|
||||||
|
MultiPartRequest reqBody = multiPartBuilder.build();
|
||||||
|
|
||||||
|
// Upload text content
|
||||||
|
HttpResponse response = post(getNodeChildrenUrl(Nodes.PATH_MY), reqBody.getBody(), null, reqBody.getContentType(), 201);
|
||||||
|
Document document = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
|
||||||
|
|
||||||
|
String contentNodeId = document.getId();
|
||||||
|
|
||||||
|
// Check the upload response
|
||||||
|
assertEquals(fileName, document.getName());
|
||||||
|
ContentInfo contentInfo = document.getContent();
|
||||||
|
assertNotNull(contentInfo);
|
||||||
|
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType());
|
||||||
|
|
||||||
|
// Download text content
|
||||||
|
response = getSingle(NodesEntityResource.class, contentNodeId + "/content", null, 200);
|
||||||
|
|
||||||
|
byte[] bytes = response.getResponseAsBytes();
|
||||||
|
assertEquals(contentSize.intValue(), bytes.length);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests download of file/content - basic read permission
|
* Tests download of file/content - basic read permission
|
||||||
* <p>GET:</p>
|
* <p>GET:</p>
|
||||||
|
Reference in New Issue
Block a user