Пример #1
0
        /// <summary>
        /// 删除指定相对路径的文件。
        /// </summary>
        /// <param name="path">指定要删除的文件的相对路径或绝对路径(绝对路径以/斜杠打头)。</param>
        public Task <bool> Delete(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            return(_accessor.Delete(path));
        }
        public async Task <IEnumerable <File> > Upload(uint?id = null)
        {
            var files = new List <File>();
            var infos = await _accessor.Write(this.Request,
                                              this.DataService.GetDirectory(id),
                                              args => args.FileName = (DateTime.Now - EPOCH).Days.ToString() + "-" + Zongsoft.Common.Randomizer.GenerateString());

            foreach (var info in infos)
            {
                if (info == null || !info.IsFile)
                {
                    continue;
                }

                object name = null;

                if (info.HasProperties && !info.Properties.TryGetValue("FileName", out name))
                {
                    info.Properties.TryGetValue("DispositionName", out name);
                }

                if (string.IsNullOrWhiteSpace(name as string))
                {
                    name = info.Name;
                }

                var attachment = Zongsoft.Data.Model.Build <File>(file =>
                {
                    file.FolderId = id.HasValue ? id.Value : 0;
                    file.Name     = info.Name;
                    file.Path     = info.Path.Url;
                    file.Type     = info.Type;
                    file.Size     = (uint)Math.Max(0, info.Size);
                });

                if (this.DataService.Insert(attachment) > 0)
                {
                    files.Add(attachment);
                }
                else
                {
                    await _accessor.Delete(info.Path.FullPath);
                }
            }

            return(files);
        }