Ejemplo n.º 1
0
        /// <summary>
        /// 上传文件。
        /// </summary>
        /// <param name="file">表单文件。</param>
        /// <param name="init">实例化媒体文件属性。</param>
        /// <param name="unique">每一个文件和媒体存储文件一一对应。</param>
        /// <returns>返回上传后的结果!</returns>
        public virtual async Task <MediaResult> UploadAsync(IFormFile file, Action <MediaFile> init, bool unique = true)
        {
            if (file == null || file.Length == 0)
            {
                return(new MediaResult(null, Resources.FormFileInvalid));
            }
            var tempFile = await _directory.SaveToTempAsync(file);

            return(await SaveAsync(tempFile, file.FileName, init, unique));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostUploadAsync(IFormFile file, string bookid)
        {
            var epub = _epubManager.Create(bookid);

            if (Path.GetExtension(file.FileName).ToLower() == ".txt")
            {
                var chapters = await _epubManager.LoadFileAsync(file);

                await epub.AddChaptersAsync(chapters);

                return(Success("成功上传了文件!"));
            }

            var info = await _storageDirectory.SaveToTempAsync(file);

            if (file.FileName.IsPictureFile())
            {
                await epub.AddCoverAsync(file.FileName, info.FullName);
            }
            else
            {
                epub.AddFile(file.FileName, info.FullName);
            }
            return(Success("成功上传了文件!"));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnPostUpload(IFormFile file, string bookid)
        {
            var epub = _epubManager.Create(bookid);
            var info = await _storageDirectory.SaveToTempAsync(file);

            if (file.FileName.IsPictureFile())
            {
                var url = epub.AddCover(info.FullName, Path.GetExtension(file.FileName));
                info.Delete();
                return(Success(new { url }));
            }

            epub.AddFile(file.FileName, info.FullName);
            return(Success());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 上传用户头像。
        /// </summary>
        /// <param name="userId">用户Id。</param>
        /// <param name="file">当前用户实例。</param>
        /// <returns>返回上传任务。</returns>
        public async Task <string> UploadAsync(int userId, IFormFile file)
        {
            var tempFile = await _storageDirectory.SaveToTempAsync(file);

            tempFile = tempFile.Resize(Size, Size);
            //上传头像新文件,把老文件删除
            var path = _storageDirectory.GetPhysicalPath($"avatars/{userId}/");

            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }

            path = Path.Combine(path, "default.png").MakeDirectory();
            tempFile.MoveTo(path);
            return($"/s-avatars/{userId}.png");
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 通过文件导入敏感词汇。
        /// </summary>
        /// <param name="file">上传文件实例。</param>
        /// <returns>返回导入词汇数量。</returns>
        public virtual async Task <int> ImportAsync(IFormFile file)
        {
            var tempFile = await _storageDirectory.SaveToTempAsync(file);

            var text = await FileHelper.ReadTextAsync(tempFile.FullName);

            if (string.IsNullOrWhiteSpace(text))
            {
                return(-1);
            }
            var words = text.Trim().Split(new[] { "\r\n", "\n", "," }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(x => x.Trim())
                        .Where(x => x.Length < 32)
                        .Distinct(StringComparer.OrdinalIgnoreCase)
                        .ToList();

            if (await ImportAsync(words))
            {
                return(words.Count);
            }
            return(0);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 分析电子书文件,返回章节和文章列表。
        /// </summary>
        /// <param name="uri">电子书URL地址。</param>
        /// <param name="encoding">字符编码。</param>
        /// <param name="title">标题格式:"^.*(第[零一二三四五六七八九十百千0-9]+章)(.*?)\r?\n"。</param>
        /// <param name="minLength">内容最少字符数。</param>
        /// <returns>返回章节和文章列表。</returns>
        public async Task <IDictionary <string, string> > LoadFileAsync(Uri uri, Encoding encoding = null, Regex title = null, int minLength = 1000)
        {
            var file = await _storageDirectory.SaveToTempAsync(uri);

            return(await LoadFileAsync(file.FullName, encoding, title, minLength));
        }