4339 |
07 Feb 17 |
nicklas |
1 |
package net.sf.basedb.reggie.plugins.release; |
4339 |
07 Feb 17 |
nicklas |
2 |
|
4339 |
07 Feb 17 |
nicklas |
3 |
|
4339 |
07 Feb 17 |
nicklas |
4 |
import net.sf.basedb.core.DbControl; |
4339 |
07 Feb 17 |
nicklas |
5 |
import net.sf.basedb.core.Directory; |
4339 |
07 Feb 17 |
nicklas |
6 |
import net.sf.basedb.core.File; |
4339 |
07 Feb 17 |
nicklas |
7 |
import net.sf.basedb.core.ItemAlreadyExistsException; |
4339 |
07 Feb 17 |
nicklas |
8 |
import net.sf.basedb.core.Path; |
4339 |
07 Feb 17 |
nicklas |
9 |
import net.sf.basedb.core.plugin.ExportOutputStream; |
4339 |
07 Feb 17 |
nicklas |
10 |
|
4339 |
07 Feb 17 |
nicklas |
11 |
/** |
4339 |
07 Feb 17 |
nicklas |
An output location that write files to the BASE file system |
4339 |
07 Feb 17 |
nicklas |
within a given root directory. |
4339 |
07 Feb 17 |
nicklas |
14 |
|
4339 |
07 Feb 17 |
nicklas |
@author nicklas |
4339 |
07 Feb 17 |
nicklas |
@since 4.10 |
4339 |
07 Feb 17 |
nicklas |
17 |
*/ |
4339 |
07 Feb 17 |
nicklas |
18 |
public class BaseFileOutputLocation |
4339 |
07 Feb 17 |
nicklas |
19 |
extends OutputLocation |
4339 |
07 Feb 17 |
nicklas |
20 |
{ |
4339 |
07 Feb 17 |
nicklas |
21 |
|
4339 |
07 Feb 17 |
nicklas |
22 |
private final DbControl dc; |
4339 |
07 Feb 17 |
nicklas |
23 |
private final Directory rootDir; |
4339 |
07 Feb 17 |
nicklas |
24 |
private final Path rootPath; |
4339 |
07 Feb 17 |
nicklas |
25 |
|
4339 |
07 Feb 17 |
nicklas |
26 |
public BaseFileOutputLocation(DbControl dc, Directory rootDir) |
4339 |
07 Feb 17 |
nicklas |
27 |
{ |
4339 |
07 Feb 17 |
nicklas |
28 |
this.dc = dc; |
4339 |
07 Feb 17 |
nicklas |
29 |
this.rootDir = rootDir; |
4339 |
07 Feb 17 |
nicklas |
30 |
this.rootPath = rootDir.getPath(); |
4339 |
07 Feb 17 |
nicklas |
31 |
} |
4339 |
07 Feb 17 |
nicklas |
32 |
|
4339 |
07 Feb 17 |
nicklas |
33 |
@Override |
4427 |
27 Mar 17 |
nicklas |
34 |
public ExportOutputStream getOutputStream(String path, boolean executable) |
4339 |
07 Feb 17 |
nicklas |
35 |
{ |
4339 |
07 Feb 17 |
nicklas |
36 |
Path p = new Path(rootPath + path, Path.Type.FILE); |
4339 |
07 Feb 17 |
nicklas |
37 |
Directory d = Directory.getNew(dc, p); |
5117 |
20 Nov 18 |
nicklas |
38 |
d.setRemoved(false); |
4339 |
07 Feb 17 |
nicklas |
39 |
|
4339 |
07 Feb 17 |
nicklas |
40 |
File file = File.getFile(dc, d, p.getFilename(), true); |
4339 |
07 Feb 17 |
nicklas |
41 |
if (file.isInDatabase() && !getOverwrite() && !file.isRemoved()) |
5117 |
20 Nov 18 |
nicklas |
42 |
{ |
4339 |
07 Feb 17 |
nicklas |
// Not allowed to overwrite the existing file |
4339 |
07 Feb 17 |
nicklas |
44 |
throw new ItemAlreadyExistsException("File[path="+file.getPath()+"]"); |
4339 |
07 Feb 17 |
nicklas |
45 |
} |
4339 |
07 Feb 17 |
nicklas |
46 |
else if (!file.isInDatabase()) |
4339 |
07 Feb 17 |
nicklas |
47 |
{ |
4339 |
07 Feb 17 |
nicklas |
48 |
dc.saveItem(file); |
4339 |
07 Feb 17 |
nicklas |
49 |
} |
4339 |
07 Feb 17 |
nicklas |
50 |
return new FileExportStream(file, getCompress()); |
4339 |
07 Feb 17 |
nicklas |
51 |
} |
4339 |
07 Feb 17 |
nicklas |
52 |
|
4339 |
07 Feb 17 |
nicklas |
53 |
/** |
4339 |
07 Feb 17 |
nicklas |
Export stream that writes to the given file. |
4339 |
07 Feb 17 |
nicklas |
55 |
*/ |
4339 |
07 Feb 17 |
nicklas |
56 |
static class FileExportStream |
4339 |
07 Feb 17 |
nicklas |
57 |
extends ExportOutputStream |
4339 |
07 Feb 17 |
nicklas |
58 |
{ |
4339 |
07 Feb 17 |
nicklas |
59 |
private final File file; |
4339 |
07 Feb 17 |
nicklas |
60 |
|
4339 |
07 Feb 17 |
nicklas |
61 |
FileExportStream(File file, boolean compress) |
4339 |
07 Feb 17 |
nicklas |
62 |
{ |
4339 |
07 Feb 17 |
nicklas |
63 |
super(file.getUploadStream(false, compress)); |
4339 |
07 Feb 17 |
nicklas |
64 |
this.file = file; |
4339 |
07 Feb 17 |
nicklas |
65 |
} |
4339 |
07 Feb 17 |
nicklas |
66 |
|
4339 |
07 Feb 17 |
nicklas |
67 |
@Override |
4339 |
07 Feb 17 |
nicklas |
68 |
public void setMimeType(String mimeType) |
4339 |
07 Feb 17 |
nicklas |
69 |
{ |
4339 |
07 Feb 17 |
nicklas |
70 |
file.setMimeType(mimeType); |
4339 |
07 Feb 17 |
nicklas |
71 |
} |
4339 |
07 Feb 17 |
nicklas |
72 |
|
4339 |
07 Feb 17 |
nicklas |
73 |
@Override |
4339 |
07 Feb 17 |
nicklas |
74 |
public void setCharacterSet(String charset) |
4339 |
07 Feb 17 |
nicklas |
75 |
{ |
4339 |
07 Feb 17 |
nicklas |
76 |
file.setCharacterSet(charset); |
4339 |
07 Feb 17 |
nicklas |
77 |
} |
4339 |
07 Feb 17 |
nicklas |
78 |
|
4339 |
07 Feb 17 |
nicklas |
79 |
@Override |
4339 |
07 Feb 17 |
nicklas |
80 |
public File getFile() |
4339 |
07 Feb 17 |
nicklas |
81 |
{ |
4339 |
07 Feb 17 |
nicklas |
82 |
return file; |
4339 |
07 Feb 17 |
nicklas |
83 |
} |
4339 |
07 Feb 17 |
nicklas |
84 |
} |
4421 |
24 Mar 17 |
nicklas |
85 |
|
4421 |
24 Mar 17 |
nicklas |
86 |
@Override |
4421 |
24 Mar 17 |
nicklas |
87 |
public String getRootPath() |
4421 |
24 Mar 17 |
nicklas |
88 |
{ |
4421 |
24 Mar 17 |
nicklas |
89 |
return rootPath.toString(); |
4421 |
24 Mar 17 |
nicklas |
90 |
} |
4421 |
24 Mar 17 |
nicklas |
91 |
|
4421 |
24 Mar 17 |
nicklas |
92 |
@Override |
4421 |
24 Mar 17 |
nicklas |
93 |
public String getReleasePath() |
4421 |
24 Mar 17 |
nicklas |
94 |
{ |
4421 |
24 Mar 17 |
nicklas |
95 |
return rootPath.toString(); |
4421 |
24 Mar 17 |
nicklas |
96 |
} |
4421 |
24 Mar 17 |
nicklas |
97 |
|
4421 |
24 Mar 17 |
nicklas |
98 |
@Override |
4421 |
24 Mar 17 |
nicklas |
99 |
public String findReleasedFile(String path) |
4421 |
24 Mar 17 |
nicklas |
100 |
{ |
4421 |
24 Mar 17 |
nicklas |
101 |
return null; |
4421 |
24 Mar 17 |
nicklas |
102 |
} |
4339 |
07 Feb 17 |
nicklas |
103 |
|
4339 |
07 Feb 17 |
nicklas |
104 |
} |