-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfUtils.fs
More file actions
84 lines (71 loc) · 2.76 KB
/
Copy pathPdfUtils.fs
File metadata and controls
84 lines (71 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module PdfUtils
open System
open System.IO
open System.Diagnostics
open Suave
open Suave.Filters
open Suave.Operators
open Suave.Successful
let pathSeparator = Path.DirectorySeparatorChar.ToString()
let appDir = Directory.GetCurrentDirectory() + pathSeparator
let pdfInputDir = appDir + "pdfInput" + pathSeparator
let textOutputDir = appDir + "textOutput" + pathSeparator
let pdfBoxDir = appDir + "pdfBox" + pathSeparator
let createAppDirs() =
[pdfInputDir;textOutputDir;]
|> Seq.iter(fun x -> if not (Directory.Exists x) then Directory.CreateDirectory(x) |> ignore)
let private quoteStr stringValue =
let quoteChar = @""""
quoteChar + stringValue + quoteChar
let private extractPdfText pdfFile =
try
let pdfFileName = Path.GetFileName pdfFile
let inputFile = pdfInputDir + pdfFileName
let javaProcess = new Process()
javaProcess.StartInfo.FileName <- "java"
javaProcess.StartInfo.Arguments <- "-jar "
+ pdfBoxDir + "pdfbox-app-2.0.6.jar ExtractText "
+ quoteStr(inputFile) + " "
+ quoteStr(textOutputDir + pdfFileName + ".txt")
javaProcess.Start() |> ignore
javaProcess.WaitForExit()
// Delete file after processing.
File.Delete(inputFile)
with
| ex -> printfn "%A" ex
let private extractPdfTextAgent = MailboxProcessor.Start(fun inbox ->
let rec messageLoop() = async {
let! msg = inbox.Receive()
extractPdfText msg
return! messageLoop()
}
messageLoop()
)
let processExistingPdfFiles() =
Directory.GetFiles pdfInputDir
|> Seq.iter extractPdfTextAgent.Post
let extractText ctx = async {
let guidAndFiles = ctx.request.files
|> Seq.map(fun x -> (Guid.NewGuid().ToString(), x))
|> Seq.toArray
guidAndFiles
|> Seq.iter(fun (fileGuid, x) ->
let tempFileName = Path.GetFileName x.tempFilePath
let fileToProcess = pdfInputDir + fileGuid
File.Copy(x.tempFilePath, pdfInputDir + tempFileName)
File.Move(pdfInputDir + tempFileName, fileToProcess)
extractPdfTextAgent.Post fileToProcess
)
let guidsOnly = guidAndFiles
|> Seq.map(fun (fileGuid, x) -> fileGuid)
|> Seq.fold(fun acc x -> if (String.IsNullOrEmpty acc) then x else acc + "," + x) ""
return! Successful.OK (guidsOnly.ToString()) ctx
}
let getFileText fileName ctx = async {
let outputFile = textOutputDir + fileName + ".txt"
if File.Exists outputFile then
let fileText = File.ReadAllText outputFile
return! OK fileText ctx
else
return! RequestErrors.NOT_FOUND "File not found" ctx
}