-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocumentManager.php
185 lines (162 loc) · 3.85 KB
/
DocumentManager.php
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* CrocoDoc Processor
*/
require_once 'crocodoc/Crocodoc.php';
class DocumentManager {
var $uuids = array();
/*
* Upload File
*
* Upload a local file to Crocodoc
* @returns mixed uuid of uploaded file or boolean false
*/
public function uploadFile($filePath) {
Crocodoc::setApiToken(CROCODOC_KEY);
if (is_file($filePath)) {
$fileHandle = fopen($filePath, 'r');
$uuid = null;
try {
$uuid = CrocodocDocument::upload($fileHandle);
return $uuid;
} catch (CrocodocException $e) {
return false;
}
} else {
return false;
}
}
/*
* Check Status
*
* Check the status a file conversion
* @returns array of document details
*/
public function checkStatus($uuid) {
Crocodoc::setApiToken(CROCODOC_KEY);
$doc['uuid'] = $uuid;
try {
$status = CrocodocDocument::status($uuid);
if (empty($status['error'])) {
$doc['status'] = $status['status'];
$doc['viewable'] = $status['viewable'] ? 'true' : 'false';
$doc['message'] = 'success';
} else {
$doc['status'] = 'error';
$doc['viewable'] = 'false';
$doc['message'] = $status['error'];
}
} catch (CrocodocException $e) {
$doc['status'] = 'error';
$doc['viewable'] = 'false';
$doc['message'] = $e->errorCode.' - '.$e->getMessage();
}
return $doc;
}
/*
* Check Statuses
*
* Check the statuses of the files this instance has created
* @returns array of document details
*/
public function checkStatuses($uuids) {
Crocodoc::setApiToken(CROCODOC_KEY);
try {
$statuses = CrocodocDocument::status($uuids);
$count=0;
foreach($statuses as $status) {
$doc['uuid'] = $status['uuid'];
if (empty($status['error'])) {
$doc[$count]['status'] = $status['status'];
$doc[$count]['viewable'] = $status['viewable'] ? 'true' : 'false';
$doc[$count]['message'] = 'success';
} else {
$doc[$count]['status'] = 'error';
$doc[$count]['viewable'] = 'false';
$doc[$count]['message'] = $status['error'];
}
$count++;
}
} catch (CrocodocException $e) {
$doc['status'] = 'error';
$doc['viewable'] = 'false';
$doc['message'] = $e->errorCode.' - '.$e->getMessage();
}
return $doc;
}
/*
* Delete File
*
* Delete a file
* @returns boolean
*/
public function deleteFile($uuid) {
Crocodoc::setApiToken(CROCODOC_KEY);
try {
$deleted = CrocodocDocument::delete($uuid);
if ($deleted) {
return true;
} else {
return false;
}
} catch (CrocodocException $e) {
return false;
}
}
/*
* Download File
*
* Download a file to the browser
* @returns mixed
*/
public function downloadFile($uuid, $filename) {
Crocodoc::setApiToken(CROCODOC_KEY);
try {
$data = CrocodocDownload::document($uuid, true); #file data.
header("Content-Type: application/pdf; name=".$filename);
header("Content-Disposition: attachment; filename=".$filename);
return $data;
} catch (CrocodocException $e) {
return false;
}
}
/*
* Download Thumbnail
*
* Download a file to the browser
* @returns mixed
*/
public function getThumbnail($uuid, $width=300, $height=300) {
Crocodoc::setApiToken(CROCODOC_KEY);
try {
$data = CrocodocDownload::thumbnail($uuid, $width, $height);
return $data;
} catch (CrocodocException $e) {
return false;
}
}
/*
* View Document
*
* Download a file to the browser
* @returns mixed
*/
public function createSession($uuid) {
Crocodoc::setApiToken(CROCODOC_KEY);
$sessionKey = null;
try {
$sessionKey = CrocodocSession::create($uuid, array(
'isEditable' => false,
'filter' => 'none',
'isAdmin' => false,
'isDownloadable' => false,
'isCopyprotected' => true,
'isDemo' => false,
'sidebar' => 'none'
));
return $sessionKey;
} catch (CrocodocException $e) {
return false;
}
}
} //end of DocumentManager class