<commit_before>package be.cytomine.formats

import be.cytomine.formats.archive.ZipFormat
import be.cytomine.formats.convertable.CellSensVSIFormat
import be.cytomine.formats.convertable.DotSlideFormat

/*
 * Copyright (c) 2009-2015. Authors: see NOTICE file.
 *
 * Licensed under the GNU Lesser General Public License, Version 2.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.gnu.org/licenses/lgpl-2.1.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import be.cytomine.formats.digitalpathology.*
import be.cytomine.formats.specialtiff.BrokenTIFFFormat
import be.cytomine.formats.specialtiff.CZITIFFFormat
import be.cytomine.formats.specialtiff.HuronTIFFFormat
import be.cytomine.formats.specialtiff.PhotoshopTIFFFormat
import be.cytomine.formats.standard.*
import utils.FilesUtils

/**
 * Created by stevben on 22/04/14.
 */
public class FormatIdentifier {

    static public getAvailableArchiveFormats() {
        return [
                new ZipFormat()
        ]
    }

    static public getAvailableMultipleImageFormats() {
        return [
                //openslide compatibles formats
                new HamamatsuVMSFormat(),
                new MiraxMRXSFormat(),
        ]
    }

    static public getAvailableHierarchicalMultipleImageFormats() {
        return [
                new DotSlideFormat(),
                new CellSensVSIFormat()
        ]
    }

    static public getAvailableSingleFileImageFormats() {
        //check the extension and or content in order to identify the right Format
        return [
                //openslide compatibles formats
                new AperioSVSFormat(),
                new HamamatsuNDPIFormat(),
                new LeicaSCNFormat(),
                new SakuraSVSlideFormat(),
                new PhilipsTIFFFormat(),
                new CZITIFFFormat(),
                //common formats
                new PhotoshopTIFFFormat(),
                new HuronTIFFFormat(),
                new BrokenTIFFFormat(),
                new PlanarTIFFFormat(),
                //new OMETIFFFormat(),
                new PyramidalTIFFFormat(),
                new VentanaTIFFFormat(),
                new JPEG2000Format(),
                new JPEGFormat(),
                new PGMFormat(),
                new PNGFormat(),
                new BMPFormat()
        ]
    }

    static public def getImageFormats(String uploadedFilePath) {

        def archiveFormats = getAvailableArchiveFormats()

        archiveFormats.each {
            it.absoluteFilePath = uploadedFilePath
        }

        ArchiveFormat detectedArchiveFormat = archiveFormats.find {
            it.detect()
        }

        def imageFormats = []
        if (detectedArchiveFormat) { //archive, we need to extract and analyze the content
            def extractedFiles = detectedArchiveFormat.extract(new File(uploadedFilePath).getParent())

            def hierarchicalMultipleFileImageFormats = getAvailableHierarchicalMultipleImageFormats()
            def extractedFolder = new File(uploadedFilePath).getParentFile().absolutePath

            // if the zip contained only one folder, we go into this folder
            def folders = new File(uploadedFilePath).getParentFile().listFiles(new FileFilter() {
                @Override
                boolean accept(File pathname) {
                    // substract the folder "MACOSX"
                    if(pathname.isDirectory() && pathname.name == "__MACOSX") return false;
                    return true
                }
            });

            if (folders.size() == 1 && folders.get(0).isDirectory) {
                File subFolder = folders.get(0)
                extractedFolder = subFolder.absolutePath
            }

            hierarchicalMultipleFileImageFormats.each { imageFormat ->
                imageFormat.absoluteFilePath = extractedFolder
                if (imageFormat.detect()) {
                    imageFormats << [<commit_msg>fix bug for Dotslide zip<commit_after>package be.cytomine.formats

import be.cytomine.formats.archive.ZipFormat
import be.cytomine.formats.convertable.CellSensVSIFormat
import be.cytomine.formats.convertable.DotSlideFormat

/*
 * Copyright (c) 2009-2015. Authors: see NOTICE file.
 *
 * Licensed under the GNU Lesser General Public License, Version 2.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.gnu.org/licenses/lgpl-2.1.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import be.cytomine.formats.digitalpathology.*
import be.cytomine.formats.specialtiff.BrokenTIFFFormat
import be.cytomine.formats.specialtiff.CZITIFFFormat
import be.cytomine.formats.specialtiff.HuronTIFFFormat
import be.cytomine.formats.specialtiff.PhotoshopTIFFFormat
import be.cytomine.formats.standard.*
import utils.FilesUtils

/**
 * Created by stevben on 22/04/14.
 */
public class FormatIdentifier {

    static public getAvailableArchiveFormats() {
        return [
                new ZipFormat()
        ]
    }

    static public getAvailableMultipleImageFormats() {
        return [
                //openslide compatibles formats
                new HamamatsuVMSFormat(),
                new MiraxMRXSFormat(),
        ]
    }

    static public getAvailableHierarchicalMultipleImageFormats() {
        return [
                new DotSlideFormat(),
                new CellSensVSIFormat()
        ]
    }

    static public getAvailableSingleFileImageFormats() {
        //check the extension and or content in order to identify the right Format
        return [
                //openslide compatibles formats
                new AperioSVSFormat(),
                new HamamatsuNDPIFormat(),
                new LeicaSCNFormat(),
                new SakuraSVSlideFormat(),
                new PhilipsTIFFFormat(),
                new CZITIFFFormat(),
                //common formats
                new PhotoshopTIFFFormat(),
                new HuronTIFFFormat(),
                new BrokenTIFFFormat(),
                new PlanarTIFFFormat(),
                //new OMETIFFFormat(),
                new PyramidalTIFFFormat(),
                new VentanaTIFFFormat(),
                new JPEG2000Format(),
                new JPEGFormat(),
                new PGMFormat(),
                new PNGFormat(),
                new BMPFormat()
        ]
    }

    static public def getImageFormats(String uploadedFilePath) {

        def archiveFormats = getAvailableArchiveFormats()

        archiveFormats.each {
            it.absoluteFilePath = uploadedFilePath
        }

        ArchiveFormat detectedArchiveFormat = archiveFormats.find {
            it.detect()
        }

        def imageFormats = []
        if (detectedArchiveFormat) { //archive, we need to extract and analyze the content
            def extractedFiles = detectedArchiveFormat.extract(new File(uploadedFilePath).getParent())

            def hierarchicalMultipleFileImageFormats = getAvailableHierarchicalMultipleImageFormats()
            def extractedFolder = new File(uploadedFilePath).getParentFile().absolutePath

            // if the zip contained only one folder, we go into this folder
            def folders = new File(uploadedFilePath).getParentFile().listFiles(new FileFilter() {
                @Override
                boolean accept(File pathname) {
                    // substract the folder "MACOSX"
                    if(pathname.isDirectory() && pathname.name == "__MACOSX") return false;
                    //substract the original zip
                    if(pathname.absolutePath == uploadedFilePath) return false;
                    return true
                }
            });

            if (folders.size() == 1 && folders[0].isDirectory()) {
                File subFolder = folders[0]
                extractedFolder = subFolder.absolutePath
            }

            hierarchicalMultipleFileImageFormats.each { imageFormat ->
                imageFormat.absoluteFilePath = extractedFolder
                if (imageFormat.detect()) {
                    imageFormats << [
{'fraction_non_alphanumeric': 0.07447050785698019, 'fraction_numerical': 0.0056934639034388525, 'mean_word_length': 2.082836082836083, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3310774', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2146, 'n_words': 588}