@@ -18,41 +18,39 @@ function __construct(string $method) {
1818 global $ conf ;
1919 try {
2020 if ($ method !== 'POST ' ) throw new Exception ("Bad method. Use POST. " );
21+ if (!isset ($ _FILES ['file ' ]) || $ _FILES ['file ' ] === NULL ) throw new Exception ("No file uploaded. " );
2122
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 );
2525
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+ }
3930
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+ }
4136
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+ ]);
4444
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. " );
4946
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 ()];
5251
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 );
5654 } catch (Exception $ e ) {
5755 parent ::outputJSON (['error ' => $ e ->getMessage ()], 500 );
5856 }
0 commit comments