Exemplo n.º 1
0
        private bool CheckOwnerFileValidity(string username, string filename, int incCount)
        {
            //Database functionality
            FileDB.PrimaryKey pk = new FileDB.PrimaryKey(filename, username, incCount);
            if (FileDB.GetFileByKey(pk) == null)//FileDB.GetFileByKey(pk).Equals(null)
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        private bool CopyCommand(Socket client, string commandMessage, string username, string filename)
        {
            string ackMessage;
            bool   checkFileValid    = CheckOwnerFileValidity(GetUsernameFromFilename(filename), GetOriginalFileName(filename), GetCopyIdFromFileName(filename));
            string directoryFileName = GetDirectoryFilename(GetUsernameFromFilename(filename), GetOriginalFileName(filename), GetCopyIdFromFileName(filename));
            bool   checkFileExist    = System.IO.File.Exists(directoryFileName);

            if (checkFileValid && checkFileExist && username == GetUsernameFromFilename(filename))
            {
                ackMessage = "ACK " + commandMessage;
                SendClientMessage(client, ackMessage);
                try
                {
                    FileDB.PrimaryKey originalPk = new FileDB.PrimaryKey(GetOriginalFileName(filename), username, GetCopyIdFromFileName(filename));
                    FileDB.PrimaryKey copyPk     = new FileDB.PrimaryKey(GetOriginalFileName(filename), username);
                    File   originalFile          = FileDB.GetFileByKey(originalPk);
                    string originalFileName      = Path.Combine(fileDirectory, originalPk.ToString());
                    string copyFileName          = Path.Combine(fileDirectory, copyPk.ToString());
                    System.IO.File.Copy(originalFileName, copyFileName);
                    FileDB.InsertFile(copyPk, Path.Combine(fileDirectory, copyFileName), originalFile.FileAccessType);


                    string outMessage = "File Copy Finished";
                    logBox.AppendText("Server: " + outMessage + "\n");
                    SendClientMessage(client, outMessage);
                }
                catch (Exception except)
                {
                    logBox.AppendText("Error: during file copy operation.\n");
                    logBox.AppendText("Connection Stopped\n");
                    client.Close();
                    return(false);
                }
            }
            else
            {
                if (!checkFileValid)
                {
                    ackMessage = "ERR " + filename + " does not exist in database!";
                }
                else if (!checkFileExist)
                {
                    ackMessage = "ERR " + filename + " does not exist in directory!";
                }
                else
                {
                    ackMessage = "ERR " + filename + " belonged to someone else!";
                }

                SendClientMessage(client, ackMessage);
                logBox.AppendText("Server: " + ackMessage + "\n");
            }
            return(true);
        }
Exemplo n.º 3
0
        private bool DeleteCommand(Socket client, string commandMessage, string username, string filename)
        {
            string ackMessage;
            bool   checkFileValid    = CheckOwnerFileValidity(GetUsernameFromFilename(filename), GetOriginalFileName(filename), GetCopyIdFromFileName(filename));
            string directoryFileName = GetDirectoryFilename(GetUsernameFromFilename(filename), GetOriginalFileName(filename), GetCopyIdFromFileName(filename));
            bool   checkFileExist    = System.IO.File.Exists(directoryFileName);

            if (checkFileValid && checkFileExist && username == GetUsernameFromFilename(filename))
            {
                ackMessage = "ACK " + commandMessage;
                SendClientMessage(client, ackMessage);
                try
                {
                    System.IO.File.Delete(directoryFileName);
                    FileDB.PrimaryKey pk = new FileDB.PrimaryKey(GetOriginalFileName(filename), username, GetCopyIdFromFileName(filename));
                    FileDB.DeleteFile(pk);

                    string outMessage = "File Delete Finished";
                    logBox.AppendText("Server: " + outMessage + "\n");
                    SendClientMessage(client, outMessage);
                }
                catch (Exception except)
                {
                    logBox.AppendText("Error: during file delete operation.\n");
                    logBox.AppendText("Connection Stopped\n");
                    client.Close();
                    return(false);
                }
            }
            else
            {
                if (!checkFileValid)
                {
                    ackMessage = "ERR " + filename + " does not exist in database!";
                }
                else if (!checkFileExist)
                {
                    ackMessage = "ERR " + filename + " does not exist in directory!";
                }
                else
                {
                    ackMessage = "ERR " + filename + " belonged to someone else!";
                }

                SendClientMessage(client, ackMessage);
                logBox.AppendText("Server: " + ackMessage + "\n");
            }
            return(true);
        }
Exemplo n.º 4
0
        private bool GetFileCommand(Socket client, string commandMessage, string username, string filename)
        {
            string ackMessage = "ACK " + commandMessage;

            SendClientMessage(client, ackMessage);

            List <File> fileList;

            if (filename == "ME")
            {
                fileList = FileDB.GetFilesByOwner(username);
            }
            else
            {
                fileList = FileDB.GetFilesByAccessType();
            }

            try
            {
                int n, temp = 0;
                int fileCount = fileList.Count();
                for (int i = 0; i < fileCount; i++)
                {
                    string fileDirectoryName = fileList[i].FilePath;
                    if (!System.IO.File.Exists(fileDirectoryName))
                    {
                        temp += 1;
                    }
                }
                n = fileCount - temp;
                Byte[] fileCountBuffer = Encoding.Default.GetBytes(n.ToString());
                n = client.Send(fileCountBuffer);

                Byte[] clientAckBuffer = new Byte [64];
                n = client.Receive(clientAckBuffer);
                string clientAck = Encoding.Default.GetString(clientAckBuffer);
                clientAck = clientAck.TrimEnd('\0');
                string outTemp = "Client: " + clientAck + "\n";
                logBox.AppendText(outTemp);

                while (temp < fileCount)
                {
                    FileInfo info   = new FileInfo(fileList[temp].FilePath);
                    long     length = info.Length;

                    string tempFilename  = "Owner: " + fileList[temp].Owner + " | Name: " + fileList[temp].Counter + "." + fileList[temp].FileName + " | Size: " + length.ToString() + " | Time:" + fileList[temp].UploadDateTime.ToString() + " | Acces: " + fileList[temp].FileAccessType.ToString() + "\n";
                    Byte[] getFileBuffer = Encoding.Default.GetBytes(tempFilename);
                    n     = client.Send(getFileBuffer);
                    temp += 1;
                }

                logBox.AppendText("Server: File List Sending Finished\n");
                //Byte[] clientAckBuffer = new Byte[64];
                Array.Clear(clientAckBuffer, 0, 64);
                int    clientAckN       = client.Receive(clientAckBuffer);
                string clientAckMessage = Encoding.Default.GetString(clientAckBuffer);
                clientAckMessage = clientAckMessage.TrimEnd('\0');
                logBox.AppendText("Client: " + clientAckMessage + "\n");
            }
            catch (Exception except)
            {
                logBox.AppendText("Error: during file list sending.\n");
                logBox.AppendText("Connection Stopped \n");
                client.Close();
                return(false);
            }
            return(true);
        }
Exemplo n.º 5
0
        private bool UploadCommand(Socket client, string commandMessage, string username, string filename)
        {
            bool   fileUploadError = false;
            string ackMessage      = "ACK " + commandMessage;

            SendClientMessage(client, ackMessage);
            //TODO fix naming
            FileDB.PrimaryKey tempPk            = new FileDB.PrimaryKey(filename, username);
            string            directoryFileName = Path.Combine(fileDirectory, tempPk.ToString());
            FileStream        uploadFile        = System.IO.File.Create(directoryFileName);

            Byte[] uploadFileBuffer = new Byte[MAX_BUF];

            Byte[] fileSizeBuffer = new Byte[64];
            try
            {
                int temp = client.Receive(fileSizeBuffer);
            }
            catch
            {
                logBox.AppendText("ERROR: During File Upload\n");
                fileUploadError = true;
                uploadFile.Close();
                return(false);
            }

            ulong fileSize     = BitConverter.ToUInt64(fileSizeBuffer, 0);
            ulong numBytesRead = 0;

            //sendClientMessage(client, "ACK");
            while (fileSize > numBytesRead)
            {
                try
                {
                    int numBytes = client.Receive(uploadFileBuffer);
                    numBytesRead += (ulong)numBytes;

                    int index = Array.FindIndex(uploadFileBuffer, CheckEnd);

                    if (index > -1)
                    {
                        uploadFile.Write(uploadFileBuffer, 0, index);
                        break;
                    }
                    else
                    {
                        uploadFile.Write(uploadFileBuffer, 0, numBytes);
                    }
                }
                catch
                {
                    logBox.AppendText("ERROR: During File Upload\n");
                    uploadFile.Close();
                    fileUploadError = false;
                    break;
                }
            }
            uploadFile.Close();

            if (!fileUploadError)
            {
                FileDB.PrimaryKey newPK = new FileDB.PrimaryKey(filename, username);
                FileDB.InsertFile(newPK, Path.Combine(fileDirectory, (newPK.ToString())));
                logBox.AppendText($"File {newPK.ToString()} UPLOADED\n");
                string message = newPK.IncCount + "." + newPK.FileName + " UPLOADED";
                SendClientMessage(client, message);
            }
            return(true);
        }