Created
March 26, 2020 00:15
-
-
Save Dviejopomata/1cdb5e66803661ca636148acafb32ae3 to your computer and use it in GitHub Desktop.
Transformar de MSAccess a CSV y de SVG a PNG en varios tamaños
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.kungfusoftware.tools | |
import com.healthmarketscience.jackcess.DatabaseBuilder | |
import com.healthmarketscience.jackcess.util.ExportUtil | |
import org.apache.batik.transcoder.TranscoderInput | |
import org.apache.batik.transcoder.TranscoderOutput | |
import org.apache.batik.transcoder.image.PNGTranscoder | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.boot.autoconfigure.SpringBootApplication | |
import org.springframework.boot.runApplication | |
import org.springframework.core.io.ByteArrayResource | |
import org.springframework.core.io.Resource | |
import org.springframework.http.ResponseEntity | |
import org.springframework.stereotype.Component | |
import org.springframework.web.bind.annotation.PostMapping | |
import org.springframework.web.bind.annotation.RequestMapping | |
import org.springframework.web.bind.annotation.RequestParam | |
import org.springframework.web.bind.annotation.RestController | |
import org.springframework.web.multipart.MultipartFile | |
import java.io.BufferedWriter | |
import java.io.ByteArrayOutputStream | |
import java.io.OutputStreamWriter | |
import java.util.zip.ZipEntry | |
import java.util.zip.ZipOutputStream | |
@SpringBootApplication | |
class KungFuSoftwareToolsApplication | |
fun main(args: Array<String>) { | |
runApplication<KungFuSoftwareToolsApplication>(*args) | |
} | |
@RestController | |
class PingController { | |
@RequestMapping("/api/ping") | |
fun ping(): String { | |
return "OK" | |
} | |
} | |
@RestController | |
class MSAccessController { | |
@Autowired | |
private lateinit var msAccessService: MSAccessService | |
@PostMapping("/api/access/csv") | |
fun msAccessToZip(@RequestParam("file", required = true) file: MultipartFile): ResponseEntity<Resource> { | |
val zip = msAccessService.msAccessToZip(file.bytes) | |
zip.use { | |
return ResponseEntity.ok() | |
.body(ByteArrayResource(it.toByteArray())) | |
} | |
} | |
} | |
@RestController("/api/svg") | |
class SvgController { | |
@Autowired | |
private lateinit var svgToPngService: SvgService | |
@PostMapping | |
fun svgToPng(@RequestParam("file", required = true) file: MultipartFile): ResponseEntity<Resource> { | |
val zip = svgToPngService.svgToZip(file.bytes) | |
zip.use { | |
return ResponseEntity.ok() | |
.body(ByteArrayResource(it.toByteArray())) | |
} | |
} | |
} | |
@Component | |
class MSAccessService { | |
fun msAccessToZip(bytes: ByteArray): ByteArrayOutputStream { | |
val tempFile = createTempFile(suffix = ".accdb") | |
tempFile.writeBytes(bytes) | |
val db = DatabaseBuilder.open(tempFile) | |
val zip = ByteArrayOutputStream() | |
val out = ZipOutputStream(zip) | |
out.use { | |
db.tableNames.forEach { tableName -> | |
val csv = ByteArrayOutputStream() | |
csv.use { | |
val writer = BufferedWriter(OutputStreamWriter(csv)) | |
writer.use { | |
ExportUtil.Builder(db, tableName).setQuote('\"').setHeader(true).setDelimiter(",") | |
.exportWriter(writer) | |
} | |
val fileName = "$tableName.csv" | |
val zipEntry = ZipEntry(fileName) | |
out.putNextEntry(zipEntry) | |
out.write(csv.toByteArray()) | |
out.closeEntry() | |
} | |
} | |
} | |
return zip | |
} | |
} | |
@Component | |
class SvgService { | |
fun svgToZip(bytes: ByteArray): ByteArrayOutputStream { | |
val sizes = listOf<Float>( | |
16.0f, | |
32.0f, | |
48.0f, | |
64.0f, | |
180.0f, | |
192.0f, | |
256.0f, | |
300.0f, | |
512.0f, | |
1024.0f | |
) | |
val zip = ByteArrayOutputStream() | |
val out = ZipOutputStream(zip) | |
out.use { | |
sizes.forEach { size -> | |
val os = svgToPng(bytes, size, size) | |
os.use { | |
val fileName = "logo-${size.toInt()}x${size.toInt()}.png" | |
val zipEntry = ZipEntry(fileName) | |
out.putNextEntry(zipEntry) | |
out.write(os.toByteArray()) | |
out.closeEntry() | |
} | |
} | |
} | |
return zip | |
} | |
fun svgToPng(bytes: ByteArray, width: Float, height: Float): ByteArrayOutputStream { | |
val transcoder = PNGTranscoder() | |
transcoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, width) | |
transcoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, height) | |
val inputStream = bytes.inputStream() | |
inputStream.use { | |
val input = TranscoderInput(inputStream) | |
val ostream = ByteArrayOutputStream() | |
val output = TranscoderOutput(ostream) | |
transcoder.transcode(input, output) | |
return ostream | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment