1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Data Analytics |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the LICENSE.md file. |
7
|
|
|
* |
8
|
|
|
* @author Marcel Scherello <[email protected]> |
9
|
|
|
* @copyright 2019 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Datasource; |
13
|
|
|
|
14
|
|
|
use OCA\Analytics\Controller\DbController; |
15
|
|
|
use OCP\AppFramework\Http\NotFoundResponse; |
16
|
|
|
use OCP\Files\IRootFolder; |
17
|
|
|
use OCP\Files\NotFoundException; |
18
|
|
|
use OCP\ILogger; |
19
|
|
|
|
20
|
|
|
class FileService |
21
|
|
|
{ |
22
|
|
|
private $logger; |
23
|
|
|
private $DBController; |
24
|
|
|
private $rootFolder; |
25
|
|
|
private $userId; |
26
|
|
|
|
27
|
|
|
//private $StorageController; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
$userId, |
31
|
|
|
ILogger $logger, |
32
|
|
|
IRootFolder $rootFolder, |
33
|
|
|
DbController $DBController |
34
|
|
|
//StorageController $StorageController |
35
|
|
|
) |
36
|
|
|
{ |
37
|
|
|
$this->userId = $userId; |
38
|
|
|
$this->logger = $logger; |
39
|
|
|
$this->DBController = $DBController; |
40
|
|
|
$this->rootFolder = $rootFolder; |
41
|
|
|
//$this->StorageController = $StorageController; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the items for the selected category |
46
|
|
|
* |
47
|
|
|
* @NoAdminRequired |
48
|
|
|
* @param $datasetMetadata |
49
|
|
|
* @param null $path |
|
|
|
|
50
|
|
|
* @return array|NotFoundResponse |
51
|
|
|
* @throws NotFoundException |
52
|
|
|
*/ |
53
|
|
|
public function read($datasetMetadata = null, $path = null) |
54
|
|
|
{ |
55
|
|
|
//$this->logger->error('FileService path: ' . $datasetMetadata['link']); |
56
|
|
|
if (empty($path)) { |
57
|
|
|
$file = $this->rootFolder->getUserFolder($datasetMetadata['user_id'])->get($datasetMetadata['link']); |
58
|
|
|
} else { |
59
|
|
|
$file = $this->rootFolder->getUserFolder($this->userId)->get($path); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$data = $file->getContent(); |
|
|
|
|
63
|
|
|
$delimiter = $this->detectDelimiter($data); |
64
|
|
|
$rows = str_getcsv($data, "\n"); |
65
|
|
|
$data = array(); |
66
|
|
|
$header = array(); |
67
|
|
|
$headerrow = 0; |
68
|
|
|
|
69
|
|
|
foreach ($rows as &$row) { |
70
|
|
|
$row = str_getcsv($row, $delimiter); |
71
|
|
|
if ($headerrow === 0) { |
72
|
|
|
$header['dimension1'] = $row[0]; |
73
|
|
|
$header['dimension2'] = $row[1]; |
74
|
|
|
$header['dimension3'] = $row[2]; |
75
|
|
|
$headerrow = 1; |
76
|
|
|
} else { |
77
|
|
|
$row[2] = str_replace(',', '.', $row[2]); |
78
|
|
|
array_push($data, ['dimension1' => $row[0], 'dimension2' => $row[1], 'dimension3' => $row[2]]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
$result = [ |
82
|
|
|
'header' => $header, |
83
|
|
|
'data' => $data |
84
|
|
|
]; |
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function detectDelimiter($data) |
89
|
|
|
{ |
90
|
|
|
$delimiters = ["\t", ";", "|", ","]; |
91
|
|
|
$data_2 = array(); |
92
|
|
|
$delimiter = $delimiters[0]; |
93
|
|
|
foreach ($delimiters as $d) { |
94
|
|
|
$firstRow = str_getcsv($data, "\n")[0]; |
95
|
|
|
$data_1 = str_getcsv($firstRow, $d); |
96
|
|
|
if (sizeof($data_1) > sizeof($data_2)) { |
97
|
|
|
$delimiter = $d; |
98
|
|
|
$data_2 = $data_1; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
return $delimiter; |
102
|
|
|
} |
103
|
|
|
} |