Exemple #1
0
        /// <summary>
        /// Receiving a file
        /// </summary>
        /// <param name="message">The message to be parsed as a file part</param>
        /// <returns></returns>
        private int ReceiveFile(Message message)
        {
            try
            {
                var contact = Contacts.FirstOrDefault(p => p.ID == message.From);
                if (contact == null)
                {
                    return(0);
                }

                // Start to receive the first block
                if (message.FileOffset == 0)
                {
                    SystemMessageIn(string.Format("[img]pack://application:,,,/Resources;component/images/degrees0.png[/img] [0.00%] \"{0}\"",
                                                  message.FileName),
                                    message.From, message.Sent, message.IsEncrypted, true);
                }

                string path = Path.Combine(Settings.Directory, TEMP_FILE_DIRECTORY);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                path = Path.Combine(path, contact.Name);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                path = Path.Combine(path, message.FileName);
                byte[] bytes = Convert.FromBase64String(message.Text);

                // If such a file exists - rewrite
                if (File.Exists(path) && message.FileOffset == 0)
                {
                    File.Delete(path);
                }

                using (var stream = File.Open(path, FileMode.Append))
                    stream.Write(bytes, 0, bytes.Length);

                // Finished receiving the last block
                if (message.FileOffset + bytes.Length == message.FileLength)
                {
                    // Куда в итоге сохранен файл
                    string movePath = historyStorage.AddFileByName(contact.Name, path);

                    // Путь до файла относительно папки приложения
                    string relativePath = movePath.Replace(Settings.Directory, "");

                    // Let's make a link to a downloaded file
                    string url          = GetRelativeLink(relativePath);
                    string urlDirectory = GetRelativeLink(Path.GetDirectoryName(relativePath));

                    string messageText;
                    if (new[] { ".jpg", ".jpeg", ".png", ".gif" }.Contains(Path.GetExtension(movePath).ToLower()))
                    {
                        messageText = string.Format("[link={0}][img]{0}[/img][/link]", url);
                    }
                    else
                    {
                        messageText = string.Format("[img]pack://application:,,,/Resources;component/images/file_received.png[/img] " +
                                                    "\"[link={1}]{0}[/link]\" [link={2}]Open folder[/link]", message.FileName, url, urlDirectory);
                    }

                    SystemMessageIn(messageText, message.From, message.Sent, message.IsEncrypted, true);
                }
                else
                {
                    double part    = (double)(message.FileOffset + bytes.Length) / message.FileLength;
                    int    degrees = 30 * (int)(12 * part);
                    SystemMessageIn(string.Format("[img]pack://application:,,,/Resources;component/images/degrees{2}.png[/img] [{1:0.00%}] \"{0}\"",
                                                  message.FileName, part, degrees), message.From, message.Sent, message.IsEncrypted, false);
                }

                return(bytes.Length);
            }
            catch (Exception ex)
            {
                ex.Process(ErrorHandlingLevels.Tray, "Error while receiving file");

                try
                {
                    SystemMessageIn(string.Format("Error while receiving file \"{0}\".", message.FileName),
                                    message.From, message.Sent, message.IsEncrypted, true);
                }
                catch
                {
                    // silence
                }

                return(0);
            }
        }