Exemple #1
0
        /// <summary>
        /// 删除已上传的文件
        /// </summary>
        public async Task <bool> DeleteFile()
        {
            if (State != FileItemState.None || string.IsNullOrEmpty(ID))
            {
                Kit.Warn("当前状态不允许删除文件!");
                return(false);
            }

            bool suc = await AtFile.Delete(ID);

            if (suc)
            {
                _owner.AfterDeleteItem(this);
            }
            else
            {
                Kit.Warn("删除文件失败!");
            }
            return(suc);
        }
Exemple #2
0
        /// <summary>
        /// 执行下载
        /// </summary>
        /// <param name="p_priorThumbnail">是否优先下载缩略图</param>
        /// <param name="p_prompt">是否提示下载失败信息</param>
        /// <returns></returns>
        internal async Task <bool> Download(bool p_priorThumbnail = false, bool p_prompt = true)
        {
            if (State != FileItemState.None || string.IsNullOrEmpty(ID))
            {
                if (p_prompt)
                {
                    Kit.Warn("当前状态不可下载!");
                }
                return(false);
            }

            bool downloadThumb = false;

            if (p_priorThumbnail)
            {
                // 优先下载缩略图时,先判断是否存在
                downloadThumb = await AtFile.Exists(ID + ThumbPostfix);

                // 缩略图不存在时,若为视频文件不下载
                if (!downloadThumb && FileType != FileItemType.Image)
                {
                    return(false);
                }
            }

            string     path   = Path.Combine(Kit.CachePath, downloadThumb ? GetThumbName() : GetFileName());
            FileStream stream = null;

            try
            {
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
                stream = System.IO.File.Create(path);
            }
            catch (Exception ex)
            {
                if (p_prompt)
                {
                    Kit.Warn("创建文件出错!");
                }
                Log.Error(ex, "下载前创建文件出错!");
                return(false);
            }

            State = FileItemState.Downloading;
            DownloadInfo info = new DownloadInfo
            {
                Path      = downloadThumb ? ID + ThumbPostfix : ID,
                TgtStream = stream,
                Progress  = OnDownloadProgress,
            };

            bool suc = false;

            _ctsDownload?.Dispose();
            _ctsDownload = new CancellationTokenSource();
            try
            {
                suc = await Downloader.GetFile(info, _ctsDownload.Token);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "下载出错");
            }
            finally
            {
                _ctsDownload?.Dispose();
                info.TgtStream.Close();
                State = FileItemState.None;
            }

            if (!suc)
            {
                if (p_prompt)
                {
                    Kit.Warn(info.Error);
                }

                // 未成功,删除缓存文件,避免打开时出错
                try
                {
                    // mono中 FileInfo 的 Exists 状态不同步!
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                }
                catch { }
            }
            else
            {
                VisualStateManager.GoToState(this, "Cached", true);
            }
            return(suc);
        }
Exemple #3
0
        /// <summary>
        /// 上传成功后
        /// </summary>
        /// <param name="p_id">文件上传路径</param>
        /// <param name="p_file">已上传文件对象</param>
        /// <returns></returns>
        async Task IUploadUI.UploadSuccess(string p_id, FileData p_file)
        {
            // 更新时删除旧文件
            if (!string.IsNullOrEmpty(ID))
            {
                // 删除服务器端旧文件
                await AtFile.Delete(ID);

                // 删除本地旧文件
                Kit.DeleteCacheFile(GetFileName());
            }

            ID = p_id;

            // 若文件已在CachePath,重命名免去再次下载,如录音、拍照、录视频等临时文件已在CachePath
            string filePath = p_file.FilePath;

#if UWP
            // 为安全访问ID
            if (filePath.StartsWith("{"))
            {
                try
                {
                    var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(filePath);

                    filePath = file.Path;
                }
                catch { }
            }
#endif
            if (filePath.StartsWith(Kit.CachePath))
            {
                try
                {
                    FileInfo fi = new FileInfo(filePath);
                    if (fi.Exists)
                    {
                        // 重命名免去再次下载
                        string newPath = Path.Combine(Kit.CachePath, GetFileName());
                        fi.MoveTo(newPath);
                        UpdateCachedFlag();
                    }
                }
                catch { }
            }

            // 缩略图重命名
            if (!string.IsNullOrEmpty(p_file.ThumbPath))
            {
                try
                {
                    FileInfo fi = new FileInfo(p_file.ThumbPath);
                    if (fi.Exists)
                    {
                        string newPath = Path.Combine(Kit.CachePath, GetThumbName());
                        fi.MoveTo(newPath);
                    }
                }
                catch { }
            }
        }