-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_upload.php
executable file
·93 lines (73 loc) · 2.08 KB
/
auto_upload.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
#!/usr/bin/php -n -d error_reporting=-1 -d display_errors=1
<?php
require_once('utils.php');
require_once('lib/TFRecord.php');
if (!isset($argv[1]))
{
die("Missing path to configuration file! Please call with configuration file!\n Usage: $argv[0] /path/to/file\n");
}
$configFile = $argv[1];
if (!file_exists($configFile))
{
die("Config file doesn't exist!\n");
}
$config = parse_ini_file($configFile);
$requiredOptions = array('dbName', 'dbHost', 'uploadForm', 'uploadAttachField');
$missingOptions = array();
foreach ($requiredOptions as $requiredOption)
{
if (!isset($config[$requiredOption]))
{
$missingOptions[] = $requiredOption;
}
}
if (count($missingOptions))
{
die("The following options need to be set in your config file: " . implode(', ', $missingOptions) . ".\n");
}
$recent = array();
while(!feof(STDIN))
{
$filename = rtrim(fgets(STDIN));
if (empty($filename))
{
echo "Read empty line\n";
continue;
}
echo "Read $filename, five second timeout.\n";
sleep(5);
if (!file_exists($filename))
{
echo "File missing: $filename\n";
continue;
}
if (in_array($filename, $recent))
{
echo "Skipping $filename, recently seen.\n";
continue;
}
$recent[] = $filename;
echo $filename . "\n";
$base = basename($filename);
if (strpos($base, '.jpg') === FALSE)
{
echo "Not a .jpg file: $filename\n";
continue;
}
$record = new TFRecord($config['dbName'], $config['uploadForm']);
if (isset($config['uploadCreatedField']))
{
$record->setValue($config['uploadCreatedField'], TFRecord::TYPE_DATE, $createdDate);
}
$record->addAttachment($config['uploadAttachField'], $filename);
$data_json = $record->toJson();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['dbHost'] . '/' . $config['dbName'] . '/' . $record->getId());
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
}