<commit_before>package main

import (
	"context"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/jgoelen/libreconv/converter"
)

// DefaultTimeout for converting a document
const DefaultTimeout = 30 * time.Second

// SetupRouter configures the Gin engine
func SetupRouter() *gin.Engine {
	router := gin.Default()
	// Set a lower memory limit for multipart forms (default is 32 MiB)
	//router.MaxMultipartMemory = 8 << 20 // 8 MiB
	router.POST("/to/pdf", convertToPdf)
	return router
}

func main() {
	router := SetupRouter()
	router.Run(":8080")
}

func convertToPdf(c *gin.Context) {
	tmp, err := ioutil.TempDir("/tmp/", "libreconv")
	if err != nil {
		c.AbortWithError(500, err)
		return
	}
	defer os.Remove(tmp)
	filePath, err := saveFileToDir(c, tmp)
	if err != nil {
		c.AbortWithError(500, err)
		return
	}
	conv, err := converter.New(filePath)
	if err != nil {
		c.AbortWithError(500, err)
		return
	}
	ctx, cancel := createContext(c.Request)<commit_msg>os.Remove does not delete dir when it still contains files<commit_after>package main

import (
	"context"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/jgoelen/libreconv/converter"
)

// DefaultTimeout for converting a document
const DefaultTimeout = 30 * time.Second

// SetupRouter configures the Gin engine
func SetupRouter() *gin.Engine {
	router := gin.Default()
	// Set a lower memory limit for multipart forms (default is 32 MiB)
	//router.MaxMultipartMemory = 8 << 20 // 8 MiB
	router.POST("/to/pdf", convertToPdf)
	return router
}

func main() {
	router := SetupRouter()
	router.Run(":8080")
}

func convertToPdf(c *gin.Context) {
	tmp, err := ioutil.TempDir("/tmp/", "libreconv")
	if err != nil {
		c.AbortWithError(500, err)
		return
	}
	defer os.RemoveAll(tmp)
	filePath, err := saveFileToDir(c, tmp)
	if err != nil {
		c.AbortWithError(500, err)
		return
	}
	conv, err := converter.New(filePath)
	if err != nil {
		c.AbortWithError(500, err)
		return
	}
	ctx, cancel := createContext(c.Request)
{'fraction_non_alphanumeric': 0.14347408829174665, 'fraction_numerical': 0.02015355086372361, 'mean_word_length': 3.9058823529411764, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4270825', 'n_tokens_mistral': 839, 'n_tokens_neox': 783, 'n_words': 196}