summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/BrandingGenerator.java
blob: 5cd1a50817b87e829b26976be3258d32582a7264 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package org.openslx.taskmanager.tasks;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.FilenameUtils;
import org.openslx.satserver.util.Archive;
import org.openslx.satserver.util.Ppm;
import org.openslx.satserver.util.Util;
import org.openslx.taskmanager.api.AbstractTask;

import com.google.gson.annotations.Expose;
import com.kitfox.svg.SVGDiagram;
import com.kitfox.svg.SVGUniverse;

public class BrandingGenerator extends AbstractTask
{
	@Expose
	private String tarFile = null;

	@Expose
	private String svgFile = null;

	private Output status = new Output();

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( status );
		if ( this.svgFile == null || this.svgFile.isEmpty() ) {
			status.error = "svgFile empty";
			return false;
		}
		if ( this.tarFile == null || this.tarFile.isEmpty() ) {
			status.error = "tarFile empty";
			return false;
		}
		this.svgFile = FilenameUtils.normalize( this.svgFile );
		this.tarFile = FilenameUtils.normalize( this.tarFile );
		if ( !Util.isAllowedDir( this.svgFile ) || !Util.isAllowedDir( this.tarFile ) ) {
			status.error = "svgFile or tarFile not in allowed dir";
			return false;
		}
		return true;
	}

	@Override
	protected boolean execute()
	{
		// Try to rasterize to png first - just to test if this is really an SVG image
		// if this fails we still continue
		File f = new File( this.svgFile );
		SVGUniverse svgUniverse = new SVGUniverse();
		File ppmFile = null;
		try {
			SVGDiagram diagram = svgUniverse.getDiagram( svgUniverse.loadSVG( f.toURI().toURL() ) );
			float scaling = 1;
			if ( diagram.getHeight() > diagram.getWidth() )
				scaling = 192f / diagram.getHeight();
			else
				scaling = 192f / diagram.getWidth();
			BufferedImage bi = new BufferedImage( (int) ( diagram.getWidth() * scaling ), (int) ( diagram.getHeight() * scaling ), BufferedImage.TYPE_INT_ARGB );
			Graphics2D rasterImage = bi.createGraphics();
			rasterImage.setClip( 0, 0, bi.getWidth(), bi.getHeight() );
			//rasterImage.setBackground( Color.WHITE );
			rasterImage.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
			rasterImage.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
			rasterImage.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
			rasterImage.setRenderingHint( RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY );
			rasterImage.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON );
			AffineTransform at = new AffineTransform();
			at.setToScale( bi.getWidth() / diagram.getWidth(), bi.getWidth() / diagram.getWidth() );
			rasterImage.transform( at );
			diagram.render( rasterImage );
			rasterImage.dispose();
			try {
				File tmpFile = File.createTempFile( "bwlp-", ".png", null );
				ImageIO.write( bi, "PNG", tmpFile );
				status.pngFile = tmpFile.getAbsolutePath();
			} catch ( Exception e ) {
				status.error = "Can't create png preview: " + e.toString();
			}
			try {
				ppmFile = File.createTempFile( "bwlp-", ".ppm", null );
				Ppm.write( bi, ppmFile.getAbsolutePath(), Color.WHITE );
			} catch ( Exception e ) {
				status.error = "Can't create png preview: " + e.toString();
				ppmFile = null;
			}
		} catch ( Throwable e ) {
			status.error = "Can't rasterize image - maybe this is not a valid SVG file? - " + e.toString();
		}
		TarArchiveOutputStream tar;
		try {
			tar = Archive.createTarArchive( this.tarFile );
		} catch ( IOException e ) {
			status.error = "Can't write to " + this.tarFile + ": " + e.toString();
			return false;
		}
		Archive.tarAddFile( tar, "/etc/branding.svg", f, 0644 );
		if ( ppmFile != null )
			Archive.tarAddFile( tar, "/etc/branding.ppm", ppmFile, 0644 );
		Util.multiClose( tar );
		return true;
	}

	/**
	 * Output - contains additional status data of this task
	 */
	@SuppressWarnings( "unused" )
	private static class Output
	{
		protected String error = null;
		protected String pngFile = null;
	}

}