/// <summary> /// 删除指定的真实文件 /// </summary> /// <param name="filePath">删除此路径的文件, 相对路径</param> /// <returns>是否删除成功</returns> public static bool DeleteFile(string filePath) { try { if (IsRelativePath(filePath)) //是否带前导~/的相对路径 { filePath = ResolvePath(filePath); } else if (!Path.IsPathRooted(filePath)) //如果是相对路径就先转换成物理路径 { filePath = IOUtil.JoinPath(Globals.ApplicationPath, filePath); } if (File.Exists(filePath)) { File.Delete(filePath); } return(true); } catch { return(false); } }
private void RebuildFile(string directoryPath, string filename, string contents) { string path = IOUtil.JoinPath(directoryPath, filename); if (File.Exists(path) == false) { File.WriteAllText(path, contents, Encoding.UTF8); } }
private bool ValidateAndSavePostedFile(AuthUser operatorUser, HttpPostedFile postedFile, string errorName, string extraFileSuffix, out string fullPath) { fullPath = string.Empty; if (postedFile == null) { ThrowError(new CustomError(errorName, "请上传身份证" + (extraFileSuffix == "face"?"正面":"背面") + "扫描件")); return(false); } List <string> allowedFileType = new List <string>(new string[] { ".jpg", ".png", ".gif" }); byte[] data = new byte[postedFile.ContentLength]; if (data.Length > AllSettings.Current.NameCheckSettings.MaxIDCardFileSize) { ThrowError(new CustomError(errorName, "身份证扫描件文件大小不能超过" + ConvertUtil.FormatSize(AllSettings.Current.NameCheckSettings.MaxIDCardFileSize))); return(false); } postedFile.InputStream.Read(data, 0, data.Length); string fileType = Path.GetExtension(postedFile.FileName).ToLower(); if (!allowedFileType.Contains(fileType) || !IOUtil.IsImageFile(data, ImageFileType.GIF | ImageFileType.JPG | ImageFileType.PNG)) { ThrowError(new CustomError(errorName, "身份证扫描件格式不正确")); return(false); } string newFileName = string.Format("{0}-{1}{2}", operatorUser.UserID, extraFileSuffix, ".config"); string path = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_IDCard), operatorUser.UserID.ToString()); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); //不做异常捕获 } try { File.WriteAllBytes(IOUtil.JoinPath(path, newFileName), data); } catch (Exception ex) { ThrowError(new CustomError("发生了系统错误" + ex.Message)); return(false); } fullPath = IOUtil.JoinPath(Globals.GetVirtualPath(SystemDirecotry.Upload_IDCard), operatorUser.UserID.ToString(), newFileName); return(true); }
private void DeleteEmoticonFiles(List <string> fileUrls) { foreach (string s in fileUrls) { if (s.Contains("/") || s.Contains("\\")) { IOUtil.DeleteFile(s); } else { string str = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_Emoticons), s.Substring(0, 1), s.Substring(1, 1), s); IOUtil.DeleteFile(str); } IOUtil.DeleteFile(GetThumbFilePath(s, false)); } }
private static string Join(JoinType joinType, params string[] paths) { if (joinType == JoinType.Path) { return(IOUtil.JoinPath(paths)); } else if (joinType == JoinType.Url) { return(UrlUtil.JoinUrl(paths)); } else if (joinType == JoinType.RelativeUrl) { string temp = UrlUtil.JoinUrl(paths); return(StringUtil.StartsWith(temp, '/') ? "~" + temp : "~/" + temp); } return(string.Empty); }
public SpaceThemeCollection GetSpaceThemes() { string[] dirs = null; SpaceThemeCollection themes = new SpaceThemeCollection(); SpaceTheme theme = new SpaceTheme(); theme.Name = "默认风格"; theme.Dir = "default"; themes.Add(theme); try { dirs = Directory.GetDirectories(Globals.GetPath(SystemDirecotry.SpaceStyles)); //.ApplicationPath + "max-templates/default\\theme"); } catch { return(themes); } foreach (string dir in dirs) { string cssFilePath = IOUtil.JoinPath(dir, "style.css"); if (File.Exists(cssFilePath) && File.Exists(IOUtil.JoinPath(dir, "preview.jpg"))) { string nameLine = IOUtil.ReadFirstLine(cssFilePath, Encoding.Default); if (nameLine.StartsWith("/*") == false || nameLine.EndsWith("*/") == false) { continue; } DirectoryInfo dirInfo = new DirectoryInfo(dir); theme = new SpaceTheme(); theme.Name = nameLine.Substring(2, nameLine.Length - 4); theme.Dir = dirInfo.Name; themes.Add(theme); } } return(themes); }
public static string MapPath(string virtualPath) { string result = HostingEnvironment.MapPath(virtualPath); if (result == null) { if (StringUtil.StartsWith(virtualPath, "~/")) { result = IOUtil.JoinPath(Globals.ApplicationPath, virtualPath.Substring(2)); } else { throw new Exception("无法映射路径:" + virtualPath); } } return(result); }
/// <summary> /// 存储由头像Flash上传的临时图片 /// </summary> /// <param name="Request"></param> /// <param name="physicalFile">输出物理路径</param> /// <returns>返回临时文件的引用URL</returns> public string SaveTempAvatar(AuthUser operatorUser, HttpRequest Request, out string physicalFile) { if (operatorUser == User.Guest) { ThrowError(new NotLoginError()); physicalFile = string.Empty; return(string.Empty); } if (Request.Files.Count == 0 || Request.FilePath.Length == 0) { throw new Exception("无法得到上传的文件"); } HttpPostedFile avatarFile = Request.Files[0]; string contentType = avatarFile.ContentType; string extendName = Path.GetExtension(avatarFile.FileName).ToLower(); if (extendName != ".png" && extendName != ".gif" && extendName != ".jpg" && extendName != ".jpeg" && extendName != ".bmp") { throw new Exception("头像的文件扩展名不能是" + extendName); } string fileName = Guid.NewGuid().ToString("N") + extendName; string tempAvatarDirectory = Globals.GetPath(SystemDirecotry.Temp_Avatar); IOUtil.CreateDirectoryIfNotExists(tempAvatarDirectory); physicalFile = IOUtil.JoinPath(tempAvatarDirectory, fileName); avatarFile.SaveAs(physicalFile); return(Globals.GetVirtualPath(SystemDirecotry.Temp_Avatar, fileName)); }
public static void LogRunInfo(string info) { HttpContext context = HttpContext.Current; if (context == null) { return; } string dir = IOUtil.JoinPath(ApplicationPath, "max-temp\\run"); string filePath = IOUtil.JoinPath(dir, "log.txt"); if (context.Items["RunInfo"] == null) { try { if (Directory.Exists(dir) == false) { Directory.CreateDirectory(dir); } else if (File.Exists(filePath)) { File.Delete(filePath); } } catch { } context.Items["RunInfo"] = true; } try { using (StreamWriter writer = new StreamWriter(filePath, true, Encoding.UTF8)) { writer.WriteLine(info); writer.Close(); } } catch { } }
internal string GetThumbFilePath(string imageFilename, bool virtualPath) { string ThunmDir = "Thunmbnails"; string fileName = Path.GetFileName(imageFilename); string path; fileName = fileName.Substring(0, fileName.LastIndexOf(".")); if (virtualPath) { path = UrlUtil.JoinUrl(Globals.GetVirtualPath(SystemDirecotry.Upload_Emoticons), ThunmDir, fileName.Substring(0, 1), fileName.Substring(1, 1)); return(string.Format("{0}/{1}.png", path, fileName)); } else { path = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_Emoticons), ThunmDir, fileName.Substring(0, 1), fileName.Substring(1, 1)); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } return(string.Format("{0}\\{1}.png", path, fileName)); } }
private string BuildAvatarPath(int userID, bool isUnapprovedAvatar, UserAvatarSize?size, string extendName) { string sizeString; if (size == null) { sizeString = "{0}"; } else { sizeString = GetAvatarSizeDirectoryName(size.Value); } if (isUnapprovedAvatar) { return(IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_Avatar), Consts.User_UncheckAvatarSuffix, sizeString, GetAvatarLevel(userID, "\\", extendName))); } else { return(Globals.GetPath(SystemDirecotry.Upload_Avatar, sizeString, GetAvatarLevel(userID, "\\", extendName))); } }
public void UpdateSpaceTheme(string theme) { int userID = UserBO.Instance.GetCurrentUserID(); if (string.Compare(theme, "default") == 0) { theme = string.Empty; } string path = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.SpaceStyles), theme); if (Directory.Exists(path)) { SpaceDao.Instance.UpdateSpaceTheme(userID, theme); User user = UserBO.Instance.GetUserFromCache(userID); if (user != null) { user.SpaceTheme = theme; } } }
private static string Join(JoinType joinType, string str1, string str2, string str3) { if (joinType == JoinType.Path) { return(IOUtil.JoinPath(str1, str2, str3)); } else if (joinType == JoinType.Url) { return(UrlUtil.JoinUrl(str1, str2, str3)); } else if (joinType == JoinType.RelativeUrl) { if (StringUtil.StartsWith(str1, '/')) { return(UrlUtil.JoinUrl("~", str1, str2, str3)); } else { return(UrlUtil.JoinUrl("~/", str1, str2, str3)); } } return(string.Empty); }
public int DetectAuthenticInfo(AuthUser operatorUser, int userID, out List <string> photos) { photos = null; if (operatorUser.UserID <= 0) { ThrowError(new NotLoginError()); return(4); } if (!CanRealnameCheck(operatorUser)) { ThrowError(new NoPermissionRealnameCheckError()); return(4); } AuthenticUser userInfo = GetAuthenticUserInfo(operatorUser, userID); if (userInfo == null) { ThrowError(new CustomError("没有该用户提交的实名认证材料")); return(4); } List <byte[]> photoData; int state = DetectAuthenticInfo(userInfo.Realname, userInfo.IDNumber, out photoData); if (state == 0) { photos = new List <string>(); if (photoData != null) { string photoString = ""; string temp; string photoDirName = "Photos"; string photoPath = Globals.GetPath(SystemDirecotry.Upload_IDCard, photoDirName); string virtualPath = Globals.GetVirtualPath(SystemDirecotry.Upload_IDCard, photoDirName); if (!Directory.Exists(photoPath)) { Directory.CreateDirectory(photoPath); } for (int i = 0; i < photoData.Count; i++) { string fileName = string.Format("{0}_{1}.jpg", userInfo.IDNumber, i); if (photoString.Length > 0) { photoString += "|"; } temp = UrlUtil.JoinUrl(virtualPath, fileName); photoString += temp; photos.Add(temp); fileName = IOUtil.JoinPath(photoPath, fileName); if (!File.Exists(fileName)) { File.WriteAllBytes(fileName, photoData[i]); } if (photos.Count > 1) //多余的照片不要, 只要最多两张 { break; } } UserDao.Instance.UpdateAuthenticUserPhoto(userID, photoString, state); } } return(state); }
/// <summary> /// 返回指定系统目录的物理路径 /// </summary> /// <param name="systemDirecotry"></param> /// <param name="addPath"></param> /// <returns></returns> public static string GetPath(SystemDirecotry systemDirecotry, string addPath1, string addPath2) { string basePath = GetPath(JoinType.Path, s_ApplicationPath, systemDirecotry); return(IOUtil.JoinPath(basePath, addPath1, addPath2)); }
public EmoticonSaveStatus SaveEmoticonFile(int userID, byte[] fileData, string md5, string fileName, out string relativeUrl) { Size thumbSize = new Size(AllSettings.Current.EmoticonSettings.ThumbImageWidth, AllSettings.Current.EmoticonSettings.ThumbImageHeight); relativeUrl = ""; if (fileData != null && fileData.Length > 0) { //判断文件大小是否超出限制 if (fileData.Length > MaxEmticonFileSize(userID)) { return(EmoticonSaveStatus.FileSizeOverflow); } if (!IsAllowedFileType(fileName)) { return(EmoticonSaveStatus.Failed); } string dirLevel1, dirLevel2; string filePostfix = ".gif"; //Path.GetExtension(fileName); fileName = string.Format("{0}_{1}{2}", md5, fileData.Length, filePostfix); string directory = Globals.GetPath(SystemDirecotry.Upload_Emoticons); dirLevel1 = md5.Substring(0, 1); dirLevel2 = md5.Substring(1, 1); directory = IOUtil.JoinPath(directory, dirLevel1, dirLevel2); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string filePath = IOUtil.JoinPath(directory, fileName); if (!File.Exists(filePath)) { using (FileStream fileStream = File.Create(filePath, fileData.Length)) { fileStream.Write(fileData, 0, fileData.Length); fileStream.Seek(0, SeekOrigin.Begin); #region 生成缩略图 try { using (Bitmap bmpSource = new Bitmap(fileStream)) { using (Bitmap bmp = new Bitmap(thumbSize.Width, thumbSize.Height)) { using (Graphics g = Graphics.FromImage(bmp)) { g.Clear(Color.White); g.DrawImage(bmpSource, new Rectangle(0, 0, thumbSize.Width, thumbSize.Height)); g.Save(); } bmp.Save(GetThumbFilePath(filePath, false), ImageFormat.Png); } } } catch { } #endregion fileStream.Close(); } } relativeUrl = fileName;//直接返回文件名 return(EmoticonSaveStatus.Success); } return(EmoticonSaveStatus.Failed); }
private static string GetConfigFilePath() { return(IOUtil.JoinPath(ApplicationPath, "bbsmax.config")); }
private string BuildThumb(PhysicalFileFromTemp file) { return(string.Empty); string thumbPathRoot = IOUtil.ResolvePath(ThumbRoot); string extendName = Path.GetExtension(file.TempUploadFileName); string level1 = file.MD5.Substring(0, 1); string level2 = file.MD5.Substring(1, 1); string level3 = file.MD5.Substring(2, 1); extendName = extendName.ToLower(); string thumbFilename = string.Format("{0}_{1}.png", file.MD5, file.FileSize); string dir = IOUtil.JoinPath(thumbPathRoot, level1, level2, level3); try { if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } } catch { return(string.Empty); } string thumbFilePath = IOUtil.JoinPath(thumbPathRoot, level1, level2, level3, thumbFilename); string thumbUrl = UrlUtil.JoinUrl(ThumbRoot, level1, level2, level3, thumbFilename); switch (extendName) { case ".jpg": case ".jpge": case ".bmp": case ".png": case ".gif": Image img = null, imgThumb = null; try { img = Bitmap.FromFile(file.PhysicalFilePath); } catch // (Exception ex) { return(string.Empty); } using (imgThumb = new Bitmap(ThumbSize, ThumbSize)) { int x, y, w, h; float scale = (float)img.Width / (float)img.Height; if (img.Width > img.Height) { x = 0; w = ThumbSize; h = (int)((float)w / scale); y = (ThumbSize - h) / 2; } else if (img.Width == img.Height) { x = 0; y = 0; w = ThumbSize; h = ThumbSize; } else { y = 0; h = ThumbSize; w = (int)((float)ThumbSize * scale); x = (ThumbSize - w) / 2; } using (Graphics g = Graphics.FromImage(imgThumb)) { g.Clear(Color.White); g.DrawImage(img, new Rectangle(x, y, w, h), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel); g.Save(); } try { img.Save(thumbFilePath, System.Drawing.Imaging.ImageFormat.Png); } catch { return(string.Empty); } } img.Dispose(); return(thumbUrl); default: break; } return(string.Empty); }