我这段代码 还可以如何优化?真心请教
/// <summary> /// PDFToSWF /// </summary> /// <param name="uploadFilepath"></param> /// <param name="file"></param> public static void AsConvertFile(string uploadFilepath, string file) { if (uploadFilepath == null || string.IsNullOrEmpty(AsFileHelper.AsCheckFileType(uploadFilepath))) { return; } else { if (uploadFilepath.EndsWith(".pdf"))//如果上传的文件是PDF格式 { string swfpath = uploadFilepath.Replace(".pdf", ".swf"); if (AsConvertHelper.AsConvertToSwf(uploadFilepath, swfpath)) { GetResponseUrl(Path.GetFileName(swfpath)); } } else { string uploadFile = AsFileHelper.AsCheckFileType(uploadFilepath);//如果上传的文件不是DPF格式的文件 file = uploadFilepath.Replace(uploadFile, ".pdf"); if (!File.Exists(file)) { string PDFFolder = file.Replace("UploadFile", "PdfFile");//PDF文件保存的文件夹路径 bool isconvert = AsConvertHelper.AsConvertToPDF(uploadFilepath, PDFFolder); if (isconvert) { string swfpath = PDFFolder.Replace(".pdf", ".swf"); if (AsConvertHelper.AsConvertToSwf(PDFFolder, swfpath)) { GetResponseUrl(Path.GetFileName(swfpath)); } } } } } }
if (uploadFilepath == null || string.IsNullOrEmpty( AsFileHelper.AsCheckFileType(uploadFilepath))){ return; }替换如下:MyHelper.Assert(uploadFilepath!=null,new ArgumentNullException("errMsg1"));string _s=AsFileHelper.AsCheckFileType(uploadFilepath);MyHelper.Assert(!string.IsNullOrEmpty(_s),new ArgumentException("errMsg2"));
[解决办法]
#region private static String ConvertToPDF(String uploadFilePath) //转为PDF文件 //----------------------------------------------------- /// <summary> /// 转为PDF文件 /// </summary> /// <param name="uploadFilePath"></param> /// <returns></returns> private static String ConvertToPDF(String uploadFilePath) { String strPDFPath = String.Empty; if (AsConvertHelper.AsConvertPDF(uploadFilePath, strPDFPath)) { return strPDFPath; } return String.Empty; } //----------------------------------------------------- #endregion #region private static String ConvertToSwf(String uploadFilePath) //转为SWF文件 //----------------------------------------------------- /// <summary> /// 转为SWF文件 /// </summary> /// <param name="uploadFilePath"></param> /// <returns></returns> private static String ConvertToSwf(String uploadFilePath) { String strSwfPath = uploadFilePath.Replace(".pdf", ".swf"); if (AsConvertHelper.AsConvertToSwf(uploadFilePath, strSwfPath)) { return strSwfPath; } return String.Empty; } //----------------------------------------------------- #endregion #region public static void AsConvertFile(String uploadFilePath) //PDFToSWF //----------------------------------------------------- /// <summary> /// PDFToSWF /// </summary> /// <param name="uploadFilepath"></param> public static void AsConvertFile(String uploadFilePath) { if (String.IsNullOrEmpty(uploadFilePath) || String.IsNullOrEmpty(AsFileHelper.AsCheckFileType(uploadFilePath))) { return; } String strSwfPath = String.Empty; // 如果上传的文件是PDF格式 if (uploadFilePath.EndsWith(".pdf")) { strSwfPath = ConvertToSwf(uploadFilePath); } else { String strPDF = ConvertToPDF(uploadFilePath); if (!File.Exists(strPDF)) { strSwfPath = ConvertToSwf(strPDF); } } if (!File.Exists(strSwfPath)) { GetResponseUrl(Path.GetFileName(strSwfPath)); } } //----------------------------------------------------- #endregion
[解决办法]
分析下你的逻辑哈。。
1.是.pdf 文件 就转成.swf .....
2.不是的话 先要变成.pdf 再转成.swf
这思路不知道对不对..
如果对的话,你有些代码就是重复了..
至少这个可以重用
if (AsConvertHelper.AsConvertToSwf(PDFFolder, swfpath))
{
GetResponseUrl(Path.GetFileName(swfpath));
}
其次你不管怎样都是要转成.swf
,所以这里完全可以独立出来