Пример #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...

                using (var file = File.Open(i.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    lock (ftpcLock)
                    {
                        _sftpc.UploadFile(file, temp, true,
                                          d =>
                        {
                            ReportTransferProgress(new TransferProgressArgs((long)d - transfered, (long)d, i,
                                                                            startedOn));
                            transfered = (long)d;
                        });

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

            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);
        }