Exemple #1
0
        public static async Task DownloadFile(Dictionary <string, object> message, IFolder downloadMainFolder)
        {
            var key = (string)message["DownloadKey"];

            if (downloading.Contains(key))
            {
                return;
            }

            downloading.Add(key);

            Debug.WriteLine("Receive begin");

            int slicesCount;

            if (message["SlicesCount"] is Int64)
            {
                slicesCount = (int)(long)message["SlicesCount"];
            }
            else
            {
                slicesCount = (int)message["SlicesCount"];
            }
            var fileName = (string)message["FileName"];
            var dateModifiedMilliseconds = (long)message["DateModified"];
            var dateCreatedMilliseconds  = (long)message["DateCreated"];
            var fileSize  = (long)message["FileSize"];
            var directory = (string)message["Directory"];
            var serverIP  = (string)message["ServerIP"];

            var dateModified = DateTimeExtension.FromUnixTimeMilliseconds(dateModifiedMilliseconds);
            var dateCreated  = DateTimeExtension.FromUnixTimeMilliseconds(dateCreatedMilliseconds);

            if (downloadMainFolder == null)
            {
                await ReceiveFailed(serverIP, key, "Default downloads folder hasn't been set.");

                return;
            }

            IFolder downloadFolder = await CreateDirectoryIfNecessary(downloadMainFolder, directory);

            IFile file = await CreateFile(downloadFolder, fileName);

            if (file.Name != fileName) //File already existed, so new name generated for it. We should update database now.
            {
                await DataStorageProviders.HistoryManager.OpenAsync();

                DataStorageProviders.HistoryManager.UpdateFileName(requestGuid, fileName, file.Name, downloadFolder.Path);
                DataStorageProviders.HistoryManager.Close();

                await DataStorageProviders.HistoryManager.OpenAsync();

                var x = DataStorageProviders.HistoryManager.GetItem(requestGuid);
                var y = x.Data as ReceivedFileCollection;
                var z = y.Files[0].Name;
                var t = x.Id;
                DataStorageProviders.HistoryManager.Close();
            }

            using (var stream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
            {
                for (uint i = 0; i < slicesCount; i++)
                {
                    string url = "http://" + serverIP + ":" + Constants.CommunicationPort.ToString() + "/" + key + "/" + i + "/";

                    byte[] buffer = await DownloadDataFromUrl(url);

                    int expectedLength;
                    if (i == (slicesCount - 1))
                    {
                        expectedLength = (int)(fileSize % ((long)Constants.FileSliceMaxLength));
                    }
                    else
                    {
                        expectedLength = (int)Constants.FileSliceMaxLength;
                    }

                    if (buffer.Length != expectedLength)
                    {
                        Debug.WriteLine("Slice length violation! Will retry...");
                        i--;
                        continue;
                    }

                    await stream.WriteAsync(buffer, 0, buffer.Length);

                    InvokeProgressEvent((uint)slicesCount, i, FileTransferState.DataTransfer);
                }

                await stream.FlushAsync();
            }

            //Debug.WriteLine(dateModified);
            //Debug.WriteLine(dateCreated);

            //System.IO.File.SetLastWriteTime(file.Path, dateModified);
            //System.IO.File.SetCreationTime(file.Path, dateCreated);

            downloading.Remove(key);

            InvokeFinishedEvent((uint)slicesCount);
            await ReceiveSuccessful(serverIP, key);
        }