1 | package monogram.plugin;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.UUID;
|
---|
6 |
|
---|
7 | import org.fuin.utils4j.Utils4J;
|
---|
8 |
|
---|
9 | import monogram.restorer.XMLRestorer;
|
---|
10 | import util.FileUtil;
|
---|
11 |
|
---|
12 | public class PluginPPTX implements Plugin
|
---|
13 | {
|
---|
14 | private final File tmpdir;
|
---|
15 | private final XMLRestorer restorer;
|
---|
16 |
|
---|
17 | public PluginPPTX(File tmpdir) {
|
---|
18 | this.tmpdir = tmpdir;
|
---|
19 | restorer = new XMLRestorer();
|
---|
20 | }
|
---|
21 |
|
---|
22 | public File run(PluginConfiguration configuration)
|
---|
23 | throws Exception
|
---|
24 | {
|
---|
25 | // Create a temporary directory and unzip the file to it
|
---|
26 | File unzipDir = createTmpDirectoryInTmp();
|
---|
27 | Utils4J.unzip(configuration.getFile(), unzipDir);
|
---|
28 |
|
---|
29 | // Restore each slide
|
---|
30 | String slideDirPath = unzipDir.getCanonicalPath() + File.separator + "ppt" + File.separator + "slides";
|
---|
31 | File slideDirectory = new File(slideDirPath);
|
---|
32 | for (File slideIn : slideDirectory.listFiles())
|
---|
33 | {
|
---|
34 | if (slideIn.isDirectory())
|
---|
35 | {
|
---|
36 | continue;
|
---|
37 | }
|
---|
38 |
|
---|
39 | File slideOut = new File(slideDirPath + File.separator + "mi-tmp-slide.xml");
|
---|
40 |
|
---|
41 | restorer.restore
|
---|
42 | (
|
---|
43 | slideIn,
|
---|
44 | "utf-8",
|
---|
45 | slideOut,
|
---|
46 | configuration.getPreserveExistingMacrons(),
|
---|
47 | configuration.getMarkupChangedWords()
|
---|
48 | );
|
---|
49 |
|
---|
50 | //Delete the old slideX.xml and rename the restored document.
|
---|
51 | String slideName = slideIn.getName();
|
---|
52 | slideIn.delete();
|
---|
53 | slideOut.renameTo(new File(slideDirPath + File.separator + slideName));
|
---|
54 | }
|
---|
55 |
|
---|
56 | //Create a file to store the zipped file.
|
---|
57 | File zipFile = File.createTempFile(FileUtil.TMP_FILE_PREFIX, configuration.getFileType(), tmpdir);
|
---|
58 |
|
---|
59 | //Zip the file.
|
---|
60 | Utils4J.zipDir(unzipDir, null, zipFile);
|
---|
61 |
|
---|
62 | //Delete zip directory.
|
---|
63 | if (unzipDir.getName().startsWith(FileUtil.TMP_FILE_PREFIX))
|
---|
64 | {
|
---|
65 | FileUtil.deleteFile(unzipDir);
|
---|
66 | }
|
---|
67 |
|
---|
68 | return zipFile;
|
---|
69 | }
|
---|
70 |
|
---|
71 | private File createTmpDirectoryInTmp()
|
---|
72 | throws IOException
|
---|
73 | {
|
---|
74 | final int maxAttempts = 10;
|
---|
75 | int attempt = 0;
|
---|
76 |
|
---|
77 | do
|
---|
78 | {
|
---|
79 | String random = UUID.randomUUID().toString();
|
---|
80 | File dir = new File(this.tmpdir + File.separator + FileUtil.TMP_FILE_PREFIX + random);
|
---|
81 |
|
---|
82 | if (!dir.exists() && dir.mkdir())
|
---|
83 | {
|
---|
84 | return dir;
|
---|
85 | }
|
---|
86 | } while (++attempt < maxAttempts);
|
---|
87 |
|
---|
88 | throw new IOException("Cound not create temp directory.");
|
---|
89 | }
|
---|
90 | }
|
---|