From 835b38d602842c709eeba539eac3c462266f2e68 Mon Sep 17 00:00:00 2001
From: bfsc19 <bfsc19@inf.ufpr.br>
Date: Sun, 26 Feb 2023 23:41:54 -0300
Subject: [PATCH] =?UTF-8?q?feat:=20adicionada=20op=C3=A7=C3=A3o=20de=20mud?=
 =?UTF-8?q?ar=20nome=20do=20arquivo=20ao=20receber?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 headers/client.h |  2 ++
 headers/server.h | 26 +++++++++++++++++++-------
 2 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/headers/client.h b/headers/client.h
index 3523ef1..e960dfa 100644
--- a/headers/client.h
+++ b/headers/client.h
@@ -111,6 +111,7 @@ int client::send_frame_socket(frame *f) {
   return 1;
 }
 
+
 // Inicia a transmissao com o servidor
 int client::start_transmission() {
   cout << "\tIniciando transmissao\n";
@@ -230,6 +231,7 @@ vector<char> client::read_file(string fileName) {
   return fileData;
 }
 
+
 /**
  * @brief Send a file through the socket
  *
diff --git a/headers/server.h b/headers/server.h
index d45e9e6..e2e76cb 100644
--- a/headers/server.h
+++ b/headers/server.h
@@ -23,6 +23,7 @@
 // include local
 #include "conexao.h"
 #include "frame.h"
+#include "cores.h"
 
 #define FILE_DESTINATION "./received"
 
@@ -195,7 +196,7 @@ bool server::create_received_dir() {
   }
 
   // create the directory
-  if (mkdir(FILE_DESTINATION, 0700) == -1) {
+  if (mkdir(FILE_DESTINATION, 0777) == -1) {
     cout << "Erro ao criar o diretorio\n";
     return false;
   }
@@ -263,16 +264,22 @@ int server::receive_file_data(string fileName) {
   fileDestination.push_back('/');
   fileDestination.append(fileName);
 
+  cout << BOLDGREEN << "Criando arquivo " << BOLDYELLOW << fileDestination
+       << BOLDGREEN << ". Digite novo nome ou enter para continuar: " << RESET;
+
+  string newDestination = "";
+  getline(cin, newDestination);
+  if(!newDestination.empty() && newDestination != "\n" ) { fileDestination = newDestination; }
+  cout << YELLOW << "Criando arquivo: " << fileDestination << "\n" << RESET;
+            
   // Abre o arquivo para escrita
   ofstream file;
   file.open(fileDestination, ios::binary);
   if (!file.is_open()) {
-    cout << "Falha ao criar o arquivo. Abortado\n";
+    cout << RED << "\tFalha ao criar o arquivo. Abortado\n" << RESET;
     return 0;
   }
 
-  cout << "Arquivo criado com sucesso\n";
-
   int lastSeq = 1;
   frame *f;
 
@@ -282,7 +289,12 @@ int server::receive_file_data(string fileName) {
 
     // Fica tentando receber um frame
     f = receive_frame_socket();
-    if (f == NULL) { return 0; }
+    if (f == NULL) {
+      cout << "Erro ao receber dados do arquivo";
+      file.close();
+      remove(fileDestination.c_str());
+      return 0;
+    }
 
     cout << "Frame recebido\n";
     f->imprime(HEX);
@@ -329,11 +341,11 @@ void server::receive_midia(frame *f) {
   if (fileName.size() == 0) { return; }
 
   if ( !receive_file_data(fileName) ) {
-    cout << "Falha ao receber o arquivo\n";
+    cout << RED << "\tFalha ao receber o arquivo\n" << RESET;
     return;
   }
 
-  cout << "Arquivo recebido com sucesso\n";
+  cout << GREEN << "\tArquivo recebido com sucesso\n" << RESET;
 }
 
 // Recebe um frame do cliente
-- 
GitLab