Exemplo n.º 1
0
        public async Task <ResultEntityBase> InsertVideoAsync(Video video)
        {
            var result = new ResultEntityBase()
            {
            };
            var count = await _videoResponstory.InsertVideoAsync(video);

            if (count > 0)
            {
                return(result);
            }
            result.Status  = StatusCode.Error;
            result.Message = "视频信息插入失败";
            return(result);
        }
Exemplo n.º 2
0
 public async Task<ResultEntityBase> UpdateTag(Tag tag)
 {
     var result = new ResultEntityBase { };
     if (await _tagResponstory.UpdateTagsAsync(tag) != 0)
     {
         result.Status = StatusCode.Success;
         result.Message = "标签更新成功";
         return await Task.FromResult(result);
     }
     else
     {
         result.Status = StatusCode.Error;
         result.Message = "标签更新失败";
         return await Task.FromResult(result);
     }
 }
Exemplo n.º 3
0
 public async Task<ResultEntityBase> AddTag(Tag tag)
 {
     var result = new ResultEntityBase { };
     if (await _tagResponstory.GetTag(tag.Name)!=null)
     {
         result.Status = StatusCode.Error;
         result.Message = "当前标签已存在";
         return await Task.FromResult(result);
     }
     if (await _tagResponstory.InsertTagsAsync(tag) != 0)
     {
         result.Status = StatusCode.Success;
         result.Message = "标签添加成功";
         return await Task.FromResult(result);
     }
     else
     {
         result.Status = StatusCode.Error;
         result.Message = "标签添加失败";
         return await Task.FromResult(result);
     }
 }
Exemplo n.º 4
0
        public async Task <ResultEntityBase> UploadVideo(CancellationToken cancellationToken)
        {
            var result = new ResultEntityBase();
            var video  = new Video();

            if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
            {
                result.Status  = CommonEntity.StatusCode.Error;
                result.Message = "请求数据类型出错";
                return(await Task.FromResult(result));
            }
            var formAccumulator = new KeyValueAccumulator();

            string videoUid = Guid.NewGuid().ToString("N");

            video.Uid   = videoUid;
            video.State = 1;
            string targetFilePath = Path.Combine(Directory.GetCurrentDirectory(), $"{_iconfiguration.GetValue<string>("VirtualPath")}/{videoUid}");

            //检查相应目录
            if (!Directory.Exists(targetFilePath))
            {
                Directory.CreateDirectory(targetFilePath);
            }
            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(Request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, HttpContext.Request.Body);

            try
            {
                var section = await reader.ReadNextSectionAsync();

                while (section != null)
                {
                    ContentDispositionHeaderValue contentDisposition;
                    var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);

                    if (hasContentDispositionHeader)
                    {
                        if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                        {
                            var fileName = section.AsFileSection().FileName;
                            video.FileName   = fileName;
                            video.Name       = fileName;
                            video.UpdateUser = "******";
                            video.CreateUser = "******";
                            targetFilePath   = Path.Combine(targetFilePath, fileName);
                            if (System.IO.File.Exists(targetFilePath))
                            {
                                result.Status  = CommonEntity.StatusCode.Error;
                                result.Message = "文件已存在";
                                break;
                            }
                            using (var targetStream = System.IO.File.Create(targetFilePath))
                            {
                                await section.Body.CopyToAsync(targetStream);

                                _ilogger.LogInformation($"复制文件到该路径下 '{targetFilePath}'");
                            }
                        }
                        else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                        {
                            // Content-Disposition: form-data; name="key"
                            //
                            // value

                            // Do not limit the key name length here because the
                            // multipart headers length limit is already in effect.
                            var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                            var encoding = GetEncoding(section);
                            using (var streamReader = new StreamReader(
                                       section.Body,
                                       encoding,
                                       detectEncodingFromByteOrderMarks: true,
                                       bufferSize: 1024,
                                       leaveOpen: true))
                            {
                                // The value length limit is enforced by MultipartBodyLengthLimit
                                var value = await streamReader.ReadToEndAsync();

                                if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                                {
                                    value = String.Empty;
                                }
                                formAccumulator.Append(key.ToString(), value);

                                if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit)
                                {
                                    throw new InvalidDataException($"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded.");
                                }
                            }
                        }
                    }
                    // Drains any remaining section body that has not been consumed and
                    // reads the headers for the next section.
                    section = await reader.ReadNextSectionAsync();
                }
                video.Name      = formAccumulator.GetResults()["name"];
                video.Title     = formAccumulator.GetResults()["title"];
                video.SecertKey = videoUid;
                video.State     = Convert.ToInt32(formAccumulator.GetResults()["state"]);
                return(await Task.FromResult(await _ivideoService.InsertVideoAsync(video)));
            }
            catch (OperationCanceledException)
            {
                if (System.IO.File.Exists(targetFilePath))
                {
                    System.IO.File.Delete(targetFilePath);
                }
                result.Status  = CommonEntity.StatusCode.Error;
                result.Message = "用户取消上传操作";
                return(await Task.FromResult(result));
            }
            catch (Exception ex)
            {
                if (System.IO.File.Exists(targetFilePath))
                {
                    System.IO.File.Delete(targetFilePath);
                }
                result.Status  = CommonEntity.StatusCode.Error;
                result.Message = "取消上传操作";
                _ilogger.LogError(new EventId(), ex, ex.Message);
                return(await Task.FromResult(result));
            }
        }