Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

java - Parsing different file types

I am learning Kotlin and facing some difficulties understanding how I can proceed

Currently I have a kml file that gets sent from the front end but now I would like to accept geoJson and store this i database -> so I need to create a function in Kotlin to validate file type and based on type return the correct object.

This the function that accepts kml file and calls parseKmlToPolygons

fun parseKmlToPolygons(file: MultipartFile, applicationConfiguration: ApplicationConfiguration): Geometry {
    if (file.size > applicationConfiguration.getMaxKmlUploadFileSizeLimitInBytes()) {
        throw FileUploadSizeLimitReachedException()
    }
    return parseMultiParFileToPolygons(file.inputStream)
}

private fun parseKmlToPolygons(content: InputStream): Geometry {
    try {
        val kml = Kml.unmarshal(content) ?: throw InvalidKmlException("Failed to parse the kml file")

        return toGeometry(kml.feature)
    } catch (ex: IllegalArgumentException) {
        throw InvalidKmlException(ex.localizedMessage, ex)
    } catch (ex: InvalidGeometryException) {
        throw InvalidKmlException(ex.localizedMessage, ex)
    }
}

So I probably need to create a function that detects a correct file, but is it ok for me to return type Any here? Also, is it possible to get the type of the file from inputStream?

private fun detectFileType():Any {

}

My apologies if I am not really clear here, all I need is to replace the function that takes kml files to be able to take either kml or geoJson

Update

  //todo would be better to have detection logic separate 
private fun parseKmlToPolygons(file: MultipartFile): Geometry {
    val fileExtension: String = FilenameUtils.getExtension(file.originalFilename)
    if (fileExtension == PolygonFileType.KML.name) {
        return parseKmlToPolygons(file.inputStream)
    } else if (fileExtension == PolygonFileType.GEOJSON.name) {
        return parseKmlToPolygons(file.inputStream)
    }
    throw FormatNotSupportedException("File format is not supported")
}
question from:https://stackoverflow.com/questions/65945478/parsing-different-file-types

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually, what do you mean by the "file type"? Both types, geoJson and kml, are text files. They do not have any magic-number encoded defining the type. So, I see the following options:

  • use extension of the original file uploaded by the user. For that you could use MultipartFile.getOriginalFilename
  • use content type set by the FE when uploading the file. MultipartFile.getContentType. Most likely it won't work out of the box and you will need to adjust your frontend.
  • check actual file content. It's the most comlex option, but as the kml is xml-based and the geoJson is JSON-based it should be feasable.
  • and finally the simplest solution: create separate endpoints for both types.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...