@@ -3083,6 +3083,238 @@ limhamn::http::server::response ff::handle_api_delete_comment_file_endpoint(cons
30833083 return response;
30843084}
30853085
3086+ limhamn::http::server::response ff::handle_api_delete_file_endpoint (const limhamn::http::server::request& request, database& db) {
3087+ limhamn::http::server::response response{};
3088+ response.content_type = " application/json" ;
3089+
3090+ const auto get_username = [&request]() -> std::string {
3091+ if (request.session .find (" username" ) != request.session .end ()) {
3092+ return request.session .at (" username" );
3093+ }
3094+
3095+ try {
3096+ const auto json = nlohmann::json::parse (request.body );
3097+ if (json.find (" username" ) != json.end () && json.at (" username" ).is_string ()) {
3098+ return json.at (" username" ).get <std::string>();
3099+ }
3100+ } catch (const std::exception&) {
3101+ // ignore
3102+ }
3103+
3104+ return " " ;
3105+ };
3106+
3107+ const auto get_key = [&request]() -> std::string {
3108+ if (request.session .find (" key" ) != request.session .end ()) {
3109+ return request.session .at (" key" );
3110+ }
3111+
3112+ try {
3113+ const auto json = nlohmann::json::parse (request.body );
3114+ if (json.find (" key" ) != json.end () && json.at (" key" ).is_string ()) {
3115+ return json.at (" key" ).get <std::string>();
3116+ }
3117+ } catch (const std::exception&) {
3118+ // ignore
3119+ }
3120+
3121+ return " " ;
3122+ };
3123+
3124+ const std::string username{get_username ()};
3125+ const std::string key{get_key ()};
3126+
3127+ if (username.empty () || key.empty ()) {
3128+ #ifdef FF_DEBUG
3129+ logger.write_to_log (limhamn::logger::type::notice, " Username or key is empty.\n " );
3130+ #endif
3131+ nlohmann::json json;
3132+ json[" error_str" ] = " Username or key is empty." ;
3133+ json[" error" ] = " FF_INVALID_CREDENTIALS" ;
3134+ response.http_status = 400 ;
3135+ response.body = json.dump ();
3136+ return response;
3137+ }
3138+
3139+ if (!ff::verify_key (db, username, key)) {
3140+ #ifdef FF_DEBUG
3141+ logger.write_to_log (limhamn::logger::type::notice, " Invalid credentials.\n " );
3142+ #endif
3143+ nlohmann::json json;
3144+ json[" error_str" ] = " Invalid credentials." ;
3145+ json[" error" ] = " FF_INVALID_CREDENTIALS" ;
3146+ response.http_status = 400 ;
3147+ response.body = json.dump ();
3148+ return response;
3149+ }
3150+
3151+ nlohmann::json json;
3152+ try {
3153+ json = nlohmann::json::parse (request.body );
3154+ } catch (const std::exception&) {
3155+ nlohmann::json ret;
3156+ ret[" error_str" ] = " Invalid JSON" ;
3157+ ret[" error" ] = " FF_INVALID_JSON" ;
3158+ response.http_status = 400 ;
3159+ response.body = ret.dump ();
3160+ return response;
3161+ }
3162+
3163+ if (!json.contains (" file_identifier" ) || !json.at (" file_identifier" ).is_string ()) {
3164+ nlohmann::json ret;
3165+ ret[" error_str" ] = " file_identifier is required" ;
3166+ ret[" error" ] = " FF_INVALID_JSON" ;
3167+ response.http_status = 400 ;
3168+ response.body = ret.dump ();
3169+ return response;
3170+ }
3171+
3172+ const std::string& file_identifier = json.at (" file_identifier" ).get <std::string>();
3173+
3174+ nlohmann::json db_json;
3175+ try {
3176+ db_json = nlohmann::json::parse (ff::get_json_from_table (db, " sandbox" , " identifier" , file_identifier));
3177+ const auto & uploader = db_json.at (" uploader" ).get <std::string>();
3178+ if (username != uploader && get_user_type (db, username) != ff::UserType::Administrator) {
3179+ nlohmann::json ret;
3180+ ret[" error_str" ] = " You can only delete your own files" ;
3181+ ret[" error" ] = " FF_NOT_AUTHORIZED" ;
3182+ response.http_status = 403 ;
3183+ response.body = ret.dump ();
3184+ return response;
3185+ }
3186+
3187+ db.exec (" DELETE FROM sandbox WHERE identifier = ?" , file_identifier);
3188+ } catch (const std::exception&) {
3189+ nlohmann::json ret;
3190+ ret[" error_str" ] = " File not found" ;
3191+ ret[" error" ] = " FF_FILE_NOT_FOUND" ;
3192+ response.http_status = 404 ;
3193+ response.body = ret.dump ();
3194+ return response;
3195+ }
3196+
3197+ response.http_status = 204 ;
3198+ response.body = " " ;
3199+ return response;
3200+ }
3201+
3202+ limhamn::http::server::response ff::handle_api_delete_forwarder_endpoint (const limhamn::http::server::request& request, database& db) {
3203+ limhamn::http::server::response response{};
3204+ response.content_type = " application/json" ;
3205+
3206+ const auto get_username = [&request]() -> std::string {
3207+ if (request.session .find (" username" ) != request.session .end ()) {
3208+ return request.session .at (" username" );
3209+ }
3210+
3211+ try {
3212+ const auto json = nlohmann::json::parse (request.body );
3213+ if (json.find (" username" ) != json.end () && json.at (" username" ).is_string ()) {
3214+ return json.at (" username" ).get <std::string>();
3215+ }
3216+ } catch (const std::exception&) {
3217+ // ignore
3218+ }
3219+
3220+ return " " ;
3221+ };
3222+
3223+ const auto get_key = [&request]() -> std::string {
3224+ if (request.session .find (" key" ) != request.session .end ()) {
3225+ return request.session .at (" key" );
3226+ }
3227+
3228+ try {
3229+ const auto json = nlohmann::json::parse (request.body );
3230+ if (json.find (" key" ) != json.end () && json.at (" key" ).is_string ()) {
3231+ return json.at (" key" ).get <std::string>();
3232+ }
3233+ } catch (const std::exception&) {
3234+ // ignore
3235+ }
3236+
3237+ return " " ;
3238+ };
3239+
3240+ const std::string username{get_username ()};
3241+ const std::string key{get_key ()};
3242+
3243+ if (username.empty () || key.empty ()) {
3244+ #ifdef FF_DEBUG
3245+ logger.write_to_log (limhamn::logger::type::notice, " Username or key is empty.\n " );
3246+ #endif
3247+ nlohmann::json json;
3248+ json[" error_str" ] = " Username or key is empty." ;
3249+ json[" error" ] = " FF_INVALID_CREDENTIALS" ;
3250+ response.http_status = 400 ;
3251+ response.body = json.dump ();
3252+ return response;
3253+ }
3254+
3255+ if (!ff::verify_key (db, username, key)) {
3256+ #ifdef FF_DEBUG
3257+ logger.write_to_log (limhamn::logger::type::notice, " Invalid credentials.\n " );
3258+ #endif
3259+ nlohmann::json json;
3260+ json[" error_str" ] = " Invalid credentials." ;
3261+ json[" error" ] = " FF_INVALID_CREDENTIALS" ;
3262+ response.http_status = 400 ;
3263+ response.body = json.dump ();
3264+ return response;
3265+ }
3266+
3267+ nlohmann::json json;
3268+ try {
3269+ json = nlohmann::json::parse (request.body );
3270+ } catch (const std::exception&) {
3271+ nlohmann::json ret;
3272+ ret[" error_str" ] = " Invalid JSON" ;
3273+ ret[" error" ] = " FF_INVALID_JSON" ;
3274+ response.http_status = 400 ;
3275+ response.body = ret.dump ();
3276+ return response;
3277+ }
3278+
3279+ if (!json.contains (" forwarder_identifier" ) || !json.at (" forwarder_identifier" ).is_string ()) {
3280+ nlohmann::json ret;
3281+ ret[" error_str" ] = " forwarder_identifier is required" ;
3282+ ret[" error" ] = " FF_INVALID_JSON" ;
3283+ response.http_status = 400 ;
3284+ response.body = ret.dump ();
3285+ return response;
3286+ }
3287+
3288+ const std::string& forwarder_identifier = json.at (" forwarder_identifier" ).get <std::string>();
3289+
3290+ nlohmann::json db_json;
3291+ try {
3292+ db_json = nlohmann::json::parse (ff::get_json_from_table (db, " forwarders" , " identifier" , forwarder_identifier));
3293+ const auto & uploader = db_json.at (" uploader" ).get <std::string>();
3294+ if (username != uploader && get_user_type (db, username) != ff::UserType::Administrator) {
3295+ nlohmann::json ret;
3296+ ret[" error_str" ] = " You can only delete your own files" ;
3297+ ret[" error" ] = " FF_NOT_AUTHORIZED" ;
3298+ response.http_status = 403 ;
3299+ response.body = ret.dump ();
3300+ return response;
3301+ }
3302+
3303+ db.exec (" DELETE FROM forwarders WHERE identifier = ?" , forwarder_identifier);
3304+ } catch (const std::exception&) {
3305+ nlohmann::json ret;
3306+ ret[" error_str" ] = " File not found" ;
3307+ ret[" error" ] = " FF_FILE_NOT_FOUND" ;
3308+ response.http_status = 404 ;
3309+ response.body = ret.dump ();
3310+ return response;
3311+ }
3312+
3313+ response.http_status = 204 ;
3314+ response.body = " " ;
3315+ return response;
3316+ }
3317+
30863318limhamn::http::server::response ff::handle_api_stay_logged_in (const limhamn::http::server::request& request, database& db) {
30873319 limhamn::http::server::response response{};
30883320
0 commit comments