11package dev .vml .es .acm .core .code ;
22
33import com .fasterxml .jackson .annotation .JsonIgnore ;
4- import dev .vml .es .acm .core .gui .SpaSettings ;
5- import dev .vml .es .acm .core .osgi .OsgiContext ;
6- import dev .vml .es .acm .core .repo .RepoChunks ;
4+ import dev .vml .es .acm .core .AcmException ;
75import java .io .Closeable ;
6+ import java .io .File ;
7+ import java .io .FileInputStream ;
8+ import java .io .FileOutputStream ;
89import java .io .Flushable ;
910import java .io .IOException ;
1011import java .io .InputStream ;
1112import java .io .OutputStream ;
1213import java .io .PrintStream ;
14+ import org .apache .commons .io .FileUtils ;
1315import org .apache .commons .lang3 .StringUtils ;
14- import org .apache .sling .api .resource .ResourceResolverFactory ;
1516
1617public class FileOutput extends Output implements Flushable , Closeable {
1718
19+ private static final String TEMP_DIR = "acm/execution/file" ;
20+
21+ @ JsonIgnore
22+ private transient File tempFile ;
23+
1824 @ JsonIgnore
19- private transient RepoChunks repoChunks ;
25+ private transient FileOutputStream fileOutputStream ;
2026
2127 @ JsonIgnore
2228 private transient PrintStream printStream ;
@@ -59,28 +65,47 @@ public void setMimeType(String mimeType) {
5965 }
6066
6167 @ JsonIgnore
62- private RepoChunks getRepoChunks () {
63- if (repoChunks == null ) {
64- OsgiContext osgi = executionContext .getCodeContext ().getOsgiContext ();
65- SpaSettings spaSettings = osgi .getService (SpaSettings .class );
66- ResourceResolverFactory resolverFactory = osgi .getService (ResourceResolverFactory .class );
67- String chunkFolderPath = String .format (
68- "%s/outputs/%s" ,
69- ExecutionContext .varPath (executionContext .getId ()), StringUtils .replace (getName (), "/" , "-" ));
70- repoChunks =
71- new RepoChunks (resolverFactory , chunkFolderPath , spaSettings .getExecutionFileOutputChunkSize ());
68+ private File getTempFile () {
69+ if (tempFile == null ) {
70+ File tmpDir = FileUtils .getTempDirectory ();
71+ String contextPrefix = StringUtils .replace (executionContext .getId (), "/" , "-" );
72+ String sanitizedName = StringUtils .replace (getName (), "/" , "-" );
73+ String fileName = String .format ("%s_%s.out" , contextPrefix , sanitizedName );
74+ File result = new File (new File (tmpDir , TEMP_DIR ), fileName );
75+ File parentDir = result .getParentFile ();
76+ if (parentDir != null && !parentDir .exists ()) {
77+ try {
78+ FileUtils .forceMkdir (parentDir );
79+ } catch (IOException e ) {
80+ throw new AcmException (
81+ String .format ("Cannot create temp directory for output '%s'!" , getName ()), e );
82+ }
83+ }
84+ this .tempFile = result ;
7285 }
73- return repoChunks ;
86+ return tempFile ;
7487 }
7588
7689 @ JsonIgnore
7790 public OutputStream getOutputStream () {
78- return getRepoChunks ().getOutputStream ();
91+ if (fileOutputStream == null ) {
92+ try {
93+ fileOutputStream = new FileOutputStream (getTempFile ());
94+ } catch (IOException e ) {
95+ throw new AcmException (
96+ String .format ("Cannot create output stream for file output '%s'!" , getName ()), e );
97+ }
98+ }
99+ return fileOutputStream ;
79100 }
80101
81102 @ JsonIgnore
82103 public InputStream getInputStream () {
83- return getRepoChunks ().getInputStream ();
104+ try {
105+ return new FileInputStream (getTempFile ());
106+ } catch (IOException e ) {
107+ throw new AcmException (String .format ("Cannot read file output '%s'!" , getName ()), e );
108+ }
84109 }
85110
86111 @ JsonIgnore
@@ -96,11 +121,38 @@ public void flush() throws IOException {
96121 if (printStream != null ) {
97122 printStream .flush ();
98123 }
99- getRepoChunks ().flush ();
124+ if (fileOutputStream != null ) {
125+ fileOutputStream .flush ();
126+ }
100127 }
101128
102129 @ Override
103130 public void close () throws IOException {
104- getRepoChunks ().close ();
131+ IOException exception = null ;
132+ try {
133+ if (printStream != null ) {
134+ printStream .close ();
135+ }
136+ if (fileOutputStream != null ) {
137+ fileOutputStream .close ();
138+ }
139+ } catch (IOException e ) {
140+ exception = e ;
141+ } finally {
142+ try {
143+ if (tempFile != null && tempFile .exists ()) {
144+ FileUtils .forceDelete (tempFile );
145+ }
146+ } catch (IOException e ) {
147+ if (exception != null ) {
148+ exception .addSuppressed (e );
149+ } else {
150+ exception = e ;
151+ }
152+ }
153+ }
154+ if (exception != null ) {
155+ throw exception ;
156+ }
105157 }
106158}
0 commit comments