ARI recordings: Issue HTTP failures for recording requests with file conflicts
[asterisk/asterisk.git] / res / res_stasis_recording.c
index 575ccae..bd2177a 100644 (file)
@@ -231,10 +231,11 @@ static void *record_file(struct stasis_app_control *control,
        recording = data;
        ast_assert(recording != NULL);
 
-       ao2_lock(recording);
-       recording->state = STASIS_APP_RECORDING_STATE_RECORDING;
-       recording_publish(recording);
-       ao2_unlock(recording);
+       if (stasis_app_get_bridge(control)) {
+               ast_log(LOG_ERROR, "Cannot record channel while in bridge\n");
+               recording_fail(recording);
+               return NULL;
+       }
 
        switch (recording->options->terminate_on) {
        case STASIS_APP_RECORDING_TERMINATE_NONE:
@@ -258,6 +259,11 @@ static void *record_file(struct stasis_app_control *control,
                return NULL;
        }
 
+       ao2_lock(recording);
+       recording->state = STASIS_APP_RECORDING_STATE_RECORDING;
+       recording_publish(recording);
+       ao2_unlock(recording);
+
        ast_play_and_record_full(chan,
                NULL, /* playfile */
                recording->absolute_name,
@@ -342,6 +348,14 @@ struct stasis_app_recording *stasis_app_control_record(
        recording->control = control;
        recording->state = STASIS_APP_RECORDING_STATE_QUEUED;
 
+       if ((recording->options->if_exists == AST_RECORD_IF_EXISTS_FAIL) &&
+                       (ast_fileexists(recording->absolute_name, NULL, NULL))) {
+               ast_log(LOG_WARNING, "Recording file '%s' already exists and ifExists option is failure.\n",
+                       recording->absolute_name);
+               errno = EEXIST;
+               return NULL;
+       }
+
        {
                RAII_VAR(struct stasis_app_recording *, old_recording, NULL,
                        ao2_cleanup);
@@ -558,7 +572,8 @@ static int unload_module(void)
        return 0;
 }
 
-AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application recording support",
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Stasis application recording support",
        .load = load_module,
        .unload = unload_module,
-       .nonoptreq = "res_stasis");
+       .nonoptreq = "res_stasis",
+       .load_pri = AST_MODPRI_APP_DEPEND);