Skip to content

Commit c4c52c0

Browse files
committed
Check that uploads directory is writable.
1 parent e2f8a0b commit c4c52c0

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

main.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace po = boost::program_options;
2424
int main( int argc, char** argv )
2525
{
2626
std::string host;
27-
std::filesystem::path certPath, keyPath;
27+
std::filesystem::path certPath, keyPath, uploads;
2828
int port;
2929
Controller::Config config;
3030

@@ -37,6 +37,7 @@ int main( int argc, char** argv )
3737
( "port", po::value( &port )->default_value( 7654 ), "Port to bind to" )
3838
( "cert", po::value( &certPath )->default_value( {} ), "Path to SSL certificate" )
3939
( "key", po::value( &keyPath )->default_value( {} ), "Path to SSL key" )
40+
( "uploads", po::value( &uploads )->default_value( "uploads" ), "Folder to store uploaded files" )
4041
;
4142

4243
po::variables_map vm;
@@ -57,6 +58,27 @@ int main( int argc, char** argv )
5758
return -1;
5859
}
5960

61+
// check that uploads directory is writable (or that we can create it)
62+
std::error_code ec;
63+
if ( std::filesystem::exists( uploads, ec ) )
64+
{
65+
const auto checkDir = uploads / "check";
66+
if ( !std::filesystem::create_directory( checkDir, ec ) )
67+
{
68+
spdlog::error( "Uploads directory is not writable: {}", ec.message() );
69+
return -1;
70+
}
71+
std::filesystem::remove( checkDir, ec );
72+
}
73+
else
74+
{
75+
if ( !std::filesystem::create_directory( uploads, ec ) )
76+
{
77+
spdlog::error( "Could not create uploads directory: {}", ec.message() );
78+
return -1;
79+
}
80+
}
81+
6082
const bool useSSL = exists( certPath ) && exists( keyPath );
6183
if ( useSSL && !app().supportSSL() )
6284
{
@@ -74,6 +96,7 @@ int main( int argc, char** argv )
7496

7597
app()
7698
.setClientMaxBodySize( 1024*1024*1024 ) // 1gb
99+
.setUploadPath( uploads )
77100
.addListener( host, port, useSSL, certPath, keyPath )
78101
.registerController( std::make_shared<Controller>( config ) )
79102
.run();

0 commit comments

Comments
 (0)