• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / ro-crate-java / #444

21 May 2025 03:00PM UTC coverage: 90.716% (+1.4%) from 89.357%
#444

push

github

web-flow
Merge pull request #255 from kit-data-manager/development

Next Version (v2.1.0-rc2)

271 of 291 new or added lines in 26 files covered. (93.13%)

1964 of 2165 relevant lines covered (90.72%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.35
/src/main/java/edu/kit/datamanager/ro_crate/preview/CratePreview.java
1
package edu.kit.datamanager.ro_crate.preview;
2

3
import java.io.File;
4
import java.io.IOException;
5
import java.nio.file.Files;
6

7
import edu.kit.datamanager.ro_crate.Crate;
8
import edu.kit.datamanager.ro_crate.writer.CrateWriter;
9
import edu.kit.datamanager.ro_crate.writer.WriteFolderStrategy;
10
import net.lingala.zip4j.ZipFile;
11
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
12
import org.apache.commons.io.FileUtils;
13
import org.slf4j.LoggerFactory;
14

15
/**
16
 * Interface for the ROCrate preview. This manages the human-readable
17
 * representation of a crate.
18
 *
19
 * @author Nikola Tzotchev on 6.2.2022 г.
20
 * @author jejkal
21
 * @version 2
22
 */
23
public interface CratePreview {
24

25
    /**
26
     * Generate a preview of the crate and store it into the given target directory.
27
     * It is the caller's responsibility to handle, e.g. delete after use, the result
28
     * (The caller takes ownership of the result).
29
     * <p>
30
     * <b>IMPORTANT NOTE:</b> This method currently has a default implementation that relies
31
     * on deprecated methods. In future, you will have to implement this method directly.
32
     *
33
     * @param crate the crate to generate a preview for.
34
     * @param targetDir the target directory to store the preview in,
35
     *                 owned by the caller.
36
     * @throws IOException if an error occurs while generating the preview.
37
     */
38
    default void generate(Crate crate, File targetDir) throws IOException {
39
        // disable preview generation to avoid recursion,
40
        // as this is usually called in the process of writing a crate
41
        // (including preview)
42
        new CrateWriter<>(new WriteFolderStrategy().disablePreview())
1✔
43
                .save(crate, targetDir.getAbsolutePath());
1✔
44
        this.saveAllToFolder(targetDir);
1✔
45
        try (var stream = Files.list(targetDir.toPath())) {
1✔
46
            stream
1✔
47
                    .filter(path -> !path.getFileName().toString().equals("ro-crate-preview.html"))
1✔
48
                    .filter(path -> !path.getFileName().toString().equals("ro-crate-preview_files"))
1✔
49
                    .forEach(path -> {
1✔
50
                        try {
51
                            if (Files.isDirectory(path)) {
1✔
52
                                FileUtils.deleteDirectory(path.toFile());
1✔
53
                            } else {
54
                                Files.delete(path);
1✔
55
                            }
NEW
56
                        } catch (IOException e) {
×
57
                            // Silently ignore deletion errors
NEW
58
                            LoggerFactory.getLogger(CratePreview.class)
×
NEW
59
                                    .error("Failed to delete temporary file {}", path, e);
×
60
                        }
1✔
61
                    });
1✔
62
        }
63
    }
1✔
64

65
    /**
66
     * Takes a crate in form of a zip file and generates a preview of it,
67
     * which will be stored within the crate.
68
     *
69
     * @param zipFile the zip file with the crate, which should receive a preview.
70
     * @throws IOException if an error occurs while saving the preview
71
     *
72
     * @deprecated Use {@link #generate(Crate, File)} instead.
73
     */
74
    @Deprecated(since = "2.1.0", forRemoval = true)
75
    void saveAllToZip(ZipFile zipFile) throws IOException;
76

77
    /**
78
     * Saves the preview, given by the folder, into the given folder.
79
     *
80
     * @param folder the folder (containing a crate) to save the preview in.
81
     * @throws IOException if an error occurs while saving the preview.
82
     *
83
     * @deprecated Use {@link #generate(Crate, File)} instead.
84
     */
85
    @Deprecated(since = "2.1.0", forRemoval = true)
86
    void saveAllToFolder(File folder) throws IOException;
87

88
    /**
89
     * Saves the preview, given by the metadata, into the given stream.
90
     *
91
     * @param metadata the metadata of the crate to save the preview in.
92
     * @param stream the stream to save the preview in.
93
     * @throws IOException if an error occurs while saving the preview.
94
     *
95
     * @deprecated Use {@link #generate(Crate, File)} instead.
96
     */
97
    @Deprecated(since = "2.1.0", forRemoval = true)
98
    void saveAllToStream(String metadata, ZipOutputStream stream) throws IOException;
99

100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc