Example #1
0
        private void FileUpload(Directory_Info dir)
        {
            File_Info file = BlindNetUtil.ByteToStruct <File_Info>(socket.CryptoReceiveMsg());

            Debug.WriteLine("Start FileUpload \"{0}\"", file.name);
            MySqlCommand commander = null;

            try
            {
                string           command = "SELECT path FROM files_info WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';";
                MySqlDataAdapter adapter = new MySqlDataAdapter(command, connection);
                DataSet          dataset = new DataSet();
                adapter.Fill(dataset);
                string path = null;

                if (dataset.Tables[0].Rows.Count != 0)
                {
                    command   = "UPDATE files_info SET modified_date = NOW() WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';";
                    commander = new MySqlCommand(command, connection);
                    if (commander.ExecuteNonQuery() != 1)
                    {
                        throw new Exception();
                    }

                    path = (string)dataset.Tables[0].Rows[0]["path"];
                    File.Delete(path);
                }
                else
                {
                    command = "INSERT INTO files_info VALUES (" + 0 + ", " + dir.id + ", '" + file.name + "', DEFAULT, UPPER('" + file.type + "'), " +
                              file.size + ", NULL);";
                    commander = new MySqlCommand(command, connection);
                    if (commander.ExecuteNonQuery() != 1)
                    {
                        throw new Exception();
                    }
                }

                Debug.WriteLine("[FileUpload] Start leceiving");
                byte[] data = socket.CryptoReceiveMsg();
                Debug.WriteLine("[FileUpload] End leceiving {0} bytes", data.Length);

                command   = "SELECT MAX(id) FROM files_info;";
                commander = new MySqlCommand(command, connection);
                MySqlDataReader reader = commander.ExecuteReader();
                reader.Read();
                file.id = (uint)reader["MAX(id)"];
                reader.Close();

                if (path == null)
                {
                    command = "SELECT path FROM directorys_info WHERE id = " + dir.id + ";";
                    adapter = new MySqlDataAdapter(command, connection);
                    adapter.Fill(dataset);
                    if (dataset.Tables[0].Rows.Count != 1)
                    {
                        throw new Exception();
                    }

                    path      = (string)dataset.Tables[0].Rows[0]["path"] + file.id + ".blind";
                    command   = "UPDATE files_info SET path = '" + RemakePath(path, false) + "' WHERE dir_id = " + dir.id + " AND name = '" + file.name + "';";
                    commander = new MySqlCommand(command, connection);
                    if (commander.ExecuteNonQuery() != 1)
                    {
                        throw new Exception();
                    }
                }

                data = EncryptFile(data);
                if (data == null)
                {
                    throw new Exception();
                }
                data = BlindNetUtil.MergeArray(BitConverter.GetBytes(file.id), data);

                FileInfo   fi = new FileInfo(path);
                FileStream fs = fi.OpenWrite();
                fs.Write(data, 0, data.Length);
                fs.Close();

                UpdateModDate(dir.id);
                socket.CryptoSend(null, PacketType.OK);
                logger.Log(LogRank.INFO, "Uploaded file(" + file.id + ")");
            }
            catch (Exception ex)
            {
                socket.CryptoSend(null, PacketType.Fail);
            }
        }