1
|
|
|
/** |
2
|
|
|
* nextCloud - ocr |
3
|
|
|
* |
4
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
5
|
|
|
* later. See the COPYING file. |
6
|
|
|
* |
7
|
|
|
* @author Janis Koehr <[email protected]> |
8
|
|
|
* @copyright Janis Koehr 2017 |
9
|
|
|
*/ |
10
|
|
|
(function (OC, OCA, window, $) { |
11
|
|
|
'use strict'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Communicates with OCR backend API. |
15
|
|
|
* @public |
16
|
|
|
* @class Ocr.HttpService |
17
|
|
|
*/ |
18
|
|
|
var HttpService = function (config) { |
19
|
|
|
|
20
|
|
|
/** Constructor */ |
21
|
|
|
var ocrConfig; |
22
|
|
|
config !== undefined ? ocrConfig = config : console.error('OCR config is not defined.'); |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @private |
27
|
|
|
* @param {Object} opts (opts.data, opts.method, opts.url) |
28
|
|
|
* @returns {$.Deferred.Promise} resolve with response or reject in case of error |
29
|
|
|
*/ |
30
|
|
|
var makeRequest = function (opts) { |
31
|
|
|
return $.ajax(opts); |
32
|
|
|
} |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Load all languages that are supported form the backend. |
36
|
|
|
* @public |
37
|
|
|
* @returns {$.Deferred} |
38
|
|
|
*/ |
39
|
|
|
this.loadAvailableLanguages = function () { |
40
|
|
|
var languages = ['deu', 'eng']; |
41
|
|
|
// TODO: enable docker behavior |
42
|
|
|
/*OCP.AppConfig.getValue('ocr', 'available-languages', '', function (data) { |
43
|
|
|
languages = data.split(';'); |
44
|
|
|
});*/ |
45
|
|
|
return languages; |
46
|
|
|
} |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Triggers the OCR process for the given files and languages. |
50
|
|
|
* @public |
51
|
|
|
* @returns {$.Deferred} |
52
|
|
|
*/ |
53
|
|
|
this.process = function (selectedFiles, selectedLanguages) { |
54
|
|
|
return makeRequest({ |
55
|
|
|
method: 'POST', |
56
|
|
|
url: ocrConfig.PROCESSING_ENDPOINT, |
|
|
|
|
57
|
|
|
data: { |
58
|
|
|
files: selectedFiles, |
59
|
|
|
languages: selectedLanguages |
60
|
|
|
}, |
61
|
|
|
}); |
62
|
|
|
} |
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check for the status of the OCR process. |
66
|
|
|
* @public |
67
|
|
|
* @returns {$.Deferred} |
68
|
|
|
*/ |
69
|
|
|
this.checkStatus = function () { |
70
|
|
|
return makeRequest({ |
71
|
|
|
method: 'GET', |
72
|
|
|
url: ocrConfig.STATUS_ENDPOINT, |
|
|
|
|
73
|
|
|
}); |
74
|
|
|
} |
|
|
|
|
75
|
|
|
} |
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Init OCR HttpService |
79
|
|
|
*/ |
80
|
|
|
/** We have to be in the Files App! */ |
81
|
|
|
if (!OCA.Files) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
/** Escape when the requested file is public.php */ |
85
|
|
|
if (/(public)\.php/i.exec(window.location.href) !== null) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
/** Create namespace Ocr */ |
89
|
|
|
if (!OCA.Ocr) { |
90
|
|
|
OCA.Ocr = {}; |
91
|
|
|
} |
92
|
|
|
OCA.Ocr.HttpService = HttpService; |
93
|
|
|
|
94
|
|
|
/** global: OC, OCA */ |
95
|
|
|
})(OC, OCA, window, jQuery); |
96
|
|
|
|
This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function: