felrock commited on
Commit
531cbf9
·
unverified ·
1 Parent(s): 027d207

Change temp file name for server application (#1535)

Browse files

Avoid issue of removing file if it exists in the current working
directory

Files changed (1) hide show
  1. examples/server/server.cpp +7 -5
examples/server/server.cpp CHANGED
@@ -453,21 +453,23 @@ int main(int argc, char ** argv) {
453
  std::vector<float> pcmf32; // mono-channel F32 PCM
454
  std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM
455
 
456
- // write file to temporary file
457
- std::ofstream temp_file{filename, std::ios::binary};
 
458
  temp_file << audio_file.content;
459
  temp_file.close();
460
 
461
  // read wav content into pcmf32
462
- if (!::read_wav(filename, pcmf32, pcmf32s, params.diarize)) {
463
- fprintf(stderr, "error: failed to read WAV file '%s'\n", filename.c_str());
464
  const std::string error_resp = "{\"error\":\"failed to read WAV file\"}";
465
  res.set_content(error_resp, "application/json");
 
466
  whisper_mutex.unlock();
467
  return;
468
  }
469
  // remove temp file
470
- std::remove(filename.c_str());
471
 
472
  printf("Successfully loaded %s\n", filename.c_str());
473
 
 
453
  std::vector<float> pcmf32; // mono-channel F32 PCM
454
  std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM
455
 
456
+ // write to temporary file
457
+ const std::string temp_filename = "whisper_server_temp_file.wav";
458
+ std::ofstream temp_file{temp_filename, std::ios::binary};
459
  temp_file << audio_file.content;
460
  temp_file.close();
461
 
462
  // read wav content into pcmf32
463
+ if (!::read_wav(temp_filename, pcmf32, pcmf32s, params.diarize)) {
464
+ fprintf(stderr, "error: failed to read WAV file '%s'\n", temp_filename.c_str());
465
  const std::string error_resp = "{\"error\":\"failed to read WAV file\"}";
466
  res.set_content(error_resp, "application/json");
467
+ std::remove(temp_filename.c_str());
468
  whisper_mutex.unlock();
469
  return;
470
  }
471
  // remove temp file
472
+ std::remove(temp_filename.c_str());
473
 
474
  printf("Successfully loaded %s\n", filename.c_str());
475