@@ -18,41 +18,39 @@ function __construct(string $method) {
18
18
global $ conf ;
19
19
try {
20
20
if ($ method !== 'POST ' ) throw new Exception ("Bad method. Use POST. " );
21
+ if (!isset ($ _FILES ['file ' ]) || $ _FILES ['file ' ] === NULL ) throw new Exception ("No file uploaded. " );
21
22
22
- if (isset ($ _FILES ['file ' ]) && $ _FILES ['file ' ] !== NULL ) {
23
- $ fileArray = $ _FILES ['file ' ];
24
- $ file = new File ($ fileArray );
23
+ $ fileArray = $ _FILES ['file ' ];
24
+ $ file = new File ($ fileArray );
25
25
26
- if (Misc::getVar ('maxviews ' ) !== NULL ) $ file ->setMaxViews (Misc::getVar ('maxviews ' ));
27
- if (Misc::getVar ('password ' ) !== NULL ) $ password = Misc::getVar ('password ' );
28
- else $ password = Misc::generatePassword (6 , 20 );
29
-
30
- $ file ->setDeletionPassword (Misc::generatePassword (12 , 32 ));
31
-
32
- $ file ->setMetaData ([
33
- 'size ' => $ fileArray ['size ' ],
34
- 'name ' => rawurlencode ($ fileArray ['name ' ]),
35
- 'type ' => $ fileArray ['type ' ]
36
- ]);
37
-
38
- $ file ->setContent (file_get_contents ($ fileArray ['tmp_name ' ]));
26
+ if (Misc::getVar ('maxviews ' ) !== NULL ) {
27
+ $ file ->setMaxViews (Misc::getVar ('maxviews ' ));
28
+ $ output ['maxviews ' ] = (int )$ file ->getMaxViews ();
29
+ }
39
30
40
- DataStorage::saveFile ($ file , $ password );
31
+ if (Misc::getVar ('password ' ) !== NULL ) $ password = Misc::getVar ('password ' );
32
+ else {
33
+ $ password = Misc::generatePassword (6 , 20 );
34
+ $ output ['password ' ] = $ password ;
35
+ }
41
36
42
- // Full URI to download the file
43
- $ completeURL = sprintf ($ conf ['download-url ' ], $ file ->getID (), $ password );
37
+ $ file ->setDeletionPassword (Misc::generatePassword (12 , 32 ));
38
+ $ file ->setContent (file_get_contents ($ fileArray ['tmp_name ' ]));
39
+ $ file ->setMetaData ([
40
+ 'size ' => $ fileArray ['size ' ],
41
+ 'name ' => rawurlencode ($ fileArray ['name ' ]),
42
+ 'type ' => $ fileArray ['type ' ]
43
+ ]);
44
44
45
- $ output = [
46
- 'url ' => $ completeURL ,
47
- 'id ' => $ file ->getID (),
48
- 'deletepassword ' => $ file ->getDeletionPassword ()];
45
+ if (!DataStorage::saveFile ($ file , $ password )) throw new Exception ("File-storing failed. " );
49
46
50
- if (Misc::getVar ('password ' ) === NULL ) $ output ['password ' ] = $ password ;
51
- if ($ file ->getMaxViews () !== NULL ) $ output ['maxviews ' ] = (int )$ file ->getMaxViews ();
47
+ $ output = [
48
+ 'url ' => sprintf ($ conf ['download-url ' ], $ file ->getID (), $ password ),
49
+ 'id ' => $ file ->getID (),
50
+ 'deletepassword ' => $ file ->getDeletionPassword ()];
52
51
53
- syslog (LOG_INFO , $ output ['id ' ] . " created. " );
54
- return parent ::outputJSON ($ output , 201 );
55
- }
52
+ syslog (LOG_INFO , $ output ['id ' ] . " created. " );
53
+ return parent ::outputJSON ($ output , 201 );
56
54
} catch (Exception $ e ) {
57
55
parent ::outputJSON (['error ' => $ e ->getMessage ()], 500 );
58
56
}
0 commit comments