Search in sources :

Example 21 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project phoebus by ControlSystemStudio.

the class AttachmentsViewController method addFiles.

private void addFiles(List<File> files) {
    MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
    for (File file : files) {
        OlogAttachment ologAttachment = new OlogAttachment();
        ologAttachment.setFile(file);
        ologAttachment.setFileName(file.getName());
        String mimeType = fileTypeMap.getContentType(file.getName());
        if (mimeType.startsWith("image")) {
            ologAttachment.setContentType("image");
        } else {
            ologAttachment.setContentType("file");
        }
        attachments.add(ologAttachment);
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) OlogAttachment(org.phoebus.olog.es.api.model.OlogAttachment) File(java.io.File)

Example 22 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project LabyCookies by Lezurex.

the class WebUtils method getResource.

/**
 * Loads a file form the resource folder and gets its MIME type.
 *
 * @param path Valid path starting in resources folder
 * @return {@link Resource} object with useful information for web usage
 */
public Resource getResource(String path) {
    final ClassLoader classLoader = getClass().getClassLoader();
    final InputStream stream = classLoader.getResourceAsStream(path);
    if (stream == null) {
        throw new IllegalArgumentException("File \"" + path + "\" was not found!");
    } else {
        if (path.endsWith(".js")) {
            return new Resource(stream, "text/javascript");
        } else if (path.endsWith(".css")) {
            return new Resource(stream, "text/css");
        }
        return new Resource(stream, new MimetypesFileTypeMap().getContentType(path));
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) InputStream(java.io.InputStream)

Example 23 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project cineast by vitrivr.

the class ImageCodebookGenerator method generate.

@Override
public void generate(Path source, Path destination, int words) throws IOException {
    long start = System.currentTimeMillis();
    final Decoder<BufferedImage> decoder = new DefaultImageDecoder();
    final MimetypesFileTypeMap filetypemap = new MimetypesFileTypeMap("mime.types");
    /* Filter the list of files and aggregate it. */
    /* Prepare array dequeue. */
    ArrayDeque<Path> files = Files.walk(source).filter(path -> {
        if (decoder.supportedFiles() != null) {
            String type = filetypemap.getContentType(path.toString());
            return decoder.supportedFiles().contains(type);
        } else {
            return true;
        }
    }).collect(Collectors.toCollection(ArrayDeque::new));
    /* Prepare data-structures to track progress. */
    int max = files.size();
    int counter = 0;
    int skipped = 0;
    char[] progressBar = new char[15];
    int update = max / progressBar.length;
    /* */
    System.out.println(String.format("Creating codebook of %d words from %d files.", words, files.size()));
    /*
     * Iterates over the files Dequeue. Every element that has been processed in removed from that Dequeue.
     */
    Path path = null;
    while ((path = files.poll()) != null) {
        if (decoder.init(path, null, null)) {
            BufferedImage image = decoder.getNext();
            if (image != null) {
                this.process(image);
            } else {
                skipped++;
            }
        } else {
            skipped++;
        }
        if (counter % update == 0) {
            this.updateProgressBar(progressBar, max, counter);
        }
        System.out.print(String.format("\rAdding vectors to codebook: %d/%d files processed (%d skipped) |%s| (Memory left: %.2f/%.2f GB)", counter, max, skipped, String.valueOf(progressBar), Runtime.getRuntime().freeMemory() / 1000000.0f, Runtime.getRuntime().totalMemory() / 1000000.0f));
        counter++;
    }
    /* Dispose of unnecessary elements. */
    files = null;
    progressBar = null;
    /* Start clustering.*/
    System.out.println(String.format("\nClustering... this could take a while."));
    this.cluster.process(words);
    /* Save file...*/
    System.out.println(String.format("Saving vocabulary with %d entries.", words));
    UtilIO.save(this.cluster.getAssignment(), destination.toString());
    long duration = System.currentTimeMillis() - start;
    System.out.println(String.format("Done! Took me %dhours %dmin %dsec", TimeUnit.MILLISECONDS.toHours(duration), TimeUnit.MILLISECONDS.toMinutes(duration), TimeUnit.MILLISECONDS.toSeconds(duration)));
}
Also used : Path(java.nio.file.Path) UtilIO(boofcv.io.UtilIO) ComputeClusters(org.ddogleg.clustering.ComputeClusters) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) IOException(java.io.IOException) Decoder(org.vitrivr.cineast.core.extraction.decode.general.Decoder) Collectors(java.util.stream.Collectors) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ClusterVisualWords(boofcv.alg.bow.ClusterVisualWords) TimeUnit(java.util.concurrent.TimeUnit) ArrayDeque(java.util.ArrayDeque) DefaultImageDecoder(org.vitrivr.cineast.core.extraction.decode.image.DefaultImageDecoder) Path(java.nio.file.Path) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) DefaultImageDecoder(org.vitrivr.cineast.core.extraction.decode.image.DefaultImageDecoder) BufferedImage(java.awt.image.BufferedImage)

Example 24 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project Fame by zzzzbw.

the class BackupController method exportArticle.

@PostMapping("export/{articleId}")
public ResponseEntity<Resource> exportArticle(@PathVariable Integer articleId) throws UnsupportedEncodingException {
    Resource file = backupService.exportArticle(articleId);
    String fileName = URLEncoder.encode(file.getFilename(), "UTF-8");
    String type = new MimetypesFileTypeMap().getContentType(fileName);
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, type).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"").header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION).body(file);
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Resource(org.springframework.core.io.Resource)

Example 25 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project clearth by exactpro.

the class LogsBean method getThreadDumps.

// *** Thread dumps routines ***
public StreamedContent getThreadDumps() {
    if (!logsDir.exists())
        return null;
    try {
        if (!outputDir.exists())
            outputDir.mkdir();
        ThreadDumpGenerator threadDumpGenerator = new ThreadDumpGenerator();
        File result = threadDumpGenerator.writeThreadDump(outputDir, UserInfoUtils.getUserName() + "_thread_dump.txt");
        result.deleteOnExit();
        return new DefaultStreamedContent(new FileInputStream(result), new MimetypesFileTypeMap().getContentType(result), "thread_dump.txt");
    } catch (Exception e) {
        String msg = "Could not generate thread dumps";
        getLogger().error(msg, e);
        MessageUtils.addErrorMessage(msg, ExceptionUtils.getDetailedMessage(e));
        return null;
    }
}
Also used : DefaultStreamedContent(org.primefaces.model.DefaultStreamedContent) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ThreadDumpGenerator(com.exactprosystems.clearth.utils.ThreadDumpGenerator) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Aggregations

MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)67 File (java.io.File)22 IOException (java.io.IOException)18 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 DefaultStreamedContent (org.primefaces.model.DefaultStreamedContent)6 URL (java.net.URL)4 Path (java.nio.file.Path)4 Map (java.util.Map)4 ResourceNotFoundException (com.plivo.api.exceptions.ResourceNotFoundException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 FileNameMap (java.net.FileNameMap)3 HttpURLConnection (java.net.HttpURLConnection)3 ArrayList (java.util.ArrayList)3 MimeType (javax.activation.MimeType)3 MimeTypeParseException (javax.activation.MimeTypeParseException)3 Response (javax.ws.rs.core.Response)3 Before (org.junit.Before)3 Test (org.junit.Test)3