protected Response <ResxResult> GetResult(string resultCode, string message, ResxResult body = null) { Response <ResxResult> response = new Response <ResxResult>(); response.Head = new GMSF.HeadDefine.ResponseHead() { ResultCode = resultCode, ResultMessage = message }; response.Body = body; return(response); }
public List <ResxResult> SaveFile() { this.LoadUpfiles(); List <ResxResult> resxResults = new List <ResxResult>(); ResxResult resx = null; foreach (var file in this.files) { string newFileName = GetNewFileName(Path.GetExtension(file.FileName).ToLower()); string localPath = Path.Combine(rootDir, newFileName); string orgUrl = Path.Combine(accessDir, newFileName); //保存图片 try { file.SaveAs(localPath); resx = new ResxResult() { Success = true, OriginalUrl = orgUrl, OriginalSize = file.InputStream.Length, //PreviewUrl = orgUrl.Replace(ext, "_p" + ext), //PreviewSize = ret.NewImageSize, //Width = ret.ImageWidth, //Height = ret.ImageHeight }; } catch (Exception) { resx = new ResxResult() { Success = false }; } if (resx.Success) { //检测是否要生成预览图片 try { if (this.resxModel.IsPreview() && file.InputStream.Length > ResxConfigManager.PREVIEW_IMAGE_SIZE) { var info = GenerateImagePreview(localPath); if (info != null) { resx.PreviewUrl = info.Item1; resx.Width = info.Item2; resx.Height = info.Item3; resx.PreviewSize = GetResxLength(); } } else if (this.resxModel.IsImage()) { var info = ImageAction.GetSize(localPath); if (info != null) { resx.Width = info.Item1; resx.Height = info.Item2; } } } catch (Exception) { } } //检测是否是语音文件 //..... //检测是否是视频文件 //..... resxResults.Add(resx); } return(resxResults); }
public async Task <ResxResult> SaveStream() { provider = new MultipartFormDataMemoryStreamProvider(); await request.Content.ReadAsMultipartAsync(provider); if (provider == null || provider.FileData == null || provider.FileData.Count <= 0) { throw new Exception("上传资源不能为空"); } var fileCount = provider.FileData.Count(); if (fileCount > ResxConfigManager.RESX_MAX_COUNT) { throw new Exception("上传资源数量超过大小"); } var count = provider.FileData.Where(f => !this.resxModel.ResxTypes.Contains(Path.GetExtension(f.Headers.ContentDisposition.FileName.Replace("\"", "")))).Count(); if (count > 0) { throw new Exception("上传包含未知资源类型"); } List <Stream> streamList = new List <Stream>(); List <string> streamNames = new List <string>(); foreach (MultipartFileData file in provider.FileData) { Stream stream = ((MultipartFileDataStream)file).Stream; if (stream.Length > this.resxModel.ResxSize && this.resxModel.ResxSize > 0) { throw new Exception("上传资源超过大小"); } else { streamList.Add(stream); streamNames.Add(file.Headers.ContentDisposition.FileName.Replace("\"", "")); } } int i = 0; ResxResult result = new ResxResult() { Success = false }; foreach (Stream stream in streamList) { string localPath = Path.Combine(rootDir, streamNames[i]); using (StreamWriter sw = new StreamWriter(localPath)) { stream.CopyTo(sw.BaseStream); sw.Flush(); string orgUrl = Path.Combine(accessDir, streamNames[i]); result.Success = true; result.OriginalUrl = orgUrl; result.OriginalSize = sw.BaseStream.Length; //检测是否要生成预览图片 if (this.resxModel.IsPreview() && sw.BaseStream.Length > ResxConfigManager.PREVIEW_IMAGE_SIZE) { //log.WriterLog("检测是否要生成预览图片sw.BaseStream.Length=" + sw.BaseStream.Length + " PREVIEW_IMAGE_SIZE=" + ResxConfigManager.PREVIEW_IMAGE_SIZE); string ext = Path.GetExtension(streamNames[i]); string newLocalPath = localPath.Replace(ext, "_p" + ext); //压缩图片 ImageAction.ThumbnailResult ret = ImageAction.ThumbnailImage(localPath, newLocalPath); if (ret.Success) { result.PreviewUrl = orgUrl.Replace(ext, "_p" + ext); //result.PreviewSize = ; result.Width = ret.ImageWidth; result.Height = ret.ImageHeight; //log.WriterLog("压缩成功:" + result.PreviewUrl); } else { result.PreviewUrl = orgUrl; result.PreviewSize = sw.BaseStream.Length; //log.WriterLog("压缩失败:" + result.PreviewUrl); } } //检测是否是语音文件 //..... //检测是否是视频文件 //..... } i++; } return(result); }
public override ResxResult SaveStream() { ResxResult resx = null; if (!isExistsTempFile)//如果不存在则直接保存 { resx = base.SaveStream(); return(resx); } else { if (string.IsNullOrEmpty(tempFileName)) { throw new Exception("未找到需要合并的文件"); } } //如果存在则将上传的分片追加到临时文件末尾 FileStream appendFileStream = null; BinaryWriter writer = null; BinaryReader reader = null; try { appendFileStream = new FileStream(tempFileLocalPath, FileMode.Append, FileAccess.Write); writer = new BinaryWriter(appendFileStream); reader = new BinaryReader(file.InputStream); writer.Write(reader.ReadBytes((int)file.InputStream.Length)); resx = new ResxResult() { Success = true, OriginalUrl = accessDir + tempFileName, OriginalSize = file.InputStream.Length, }; } catch (Exception e) { resx = new ResxResult() { Success = false, Message = e.Message }; } finally { if (appendFileStream != null) { appendFileStream.Close(); appendFileStream.Dispose(); } if (writer != null) { writer.Close(); writer.Dispose(); } if (file.InputStream != null) { file.InputStream.Close(); file.InputStream.Dispose(); } if (reader != null) { reader.Close(); reader.Dispose(); } } if (chunkIndex >= chunkCount)//说明是最后一块数据流 { var fileInfo = ResetFileName(); resx.OriginalUrl = accessDir + fileInfo.Item1; resx.OriginalSize = fileInfo.Item2; //生成视频预览图片 string videoLocalPath = rootDir + fileInfo.Item1; string videoPreviewSavePath = videoLocalPath.Replace(Path.GetExtension(videoLocalPath), "_p.jpg"); var tuple = new VideoImageHanlder().CatchImg(videoLocalPath, videoPreviewSavePath); if (tuple.Item1) { resx.PreviewUrl = accessDir + Path.GetFileName(videoPreviewSavePath); resx.PlayTime = long.Parse(tuple.Item2 + ""); } } return(resx); }