Ejemplo n.º 1
0
        /// <summary>
        ///     Upload to a temporary file.
        ///     If the transfer is successful, replace the old file with the temporary one.
        ///     If not, delete the temporary file.
        /// </summary>
        /// <param name="i">The item to upload</param>
        /// <returns>TransferStatus.Success on success, TransferStatus.Success on failure</returns>
        public override TransferStatus SafeUpload(SyncQueueItem i)
        {
            // is this the first time we check the files?
            //if (_controller.FileLog.IsEmpty())
            //{
            //TODO: allow user to select if the following should happen
            // skip synchronization if the file already exists and has the exact same size
            if (Exists(i.CommonPath) && SizeOf(i.CommonPath) == i.Item.Size)
            {
                Log.Write(l.Client, "File seems to be already synced (skipping): {0}", i.CommonPath);
                return(TransferStatus.Success);
            }
            //}

            Notifications.ChangeTrayText(MessageType.Uploading, i.Item.Name);
            var temp = Common._tempName(i.CommonPath, _controller.Account.TempFilePrefix);

            try
            {
                var  startedOn  = DateTime.Now;
                long transfered = 0;
                // upload to a temp file...
                lock (ftpcLock)
                {
                    using (
                        Stream file = File.Open(i.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
                        rem = _ftpc.OpenWrite(temp))
                    {
                        var buf = new byte[8192];
                        int read;

                        while ((read = file.Read(buf, 0, buf.Length)) > 0)
                        {
                            rem.Write(buf, 0, read);
                            transfered += read;

                            ReportTransferProgress(new TransferProgressArgs(read, transfered, i, startedOn));

                            ThrottleTransfer(Settings.General.UploadLimit, transfered, startedOn);
                        }

                        Notifications.ChangeTrayText(MessageType.Size, null, i.Item.Size);
                    }
                }
            }
            catch (Exception ex)
            {
                Common.LogError(ex);
                CheckWorkingDirectory();
                return(TransferStatus.Failure);
            }

            CheckWorkingDirectory();

            Thread.Sleep(2000); //wait syncing

            long size = SizeOf(temp);

            if (i.Item.Size == size)
            {
                if (Exists(i.CommonPath))
                {
                    Remove(i.CommonPath);
                }
                Rename(temp, i.CommonPath);

                return(TransferStatus.Success);
            }
            Remove(temp);
            return(TransferStatus.Failure);
        }