private static string ReplaceUrl(Match match) { FriendlyUrlSettings settings = AllSettings.Current.FriendlyUrlSettings; string root = Globals.GetVirtualPath(SystemDirecotry.Root); string url; if (root == "/") { switch (settings.UrlFormat) { case UrlFormat.Query: url = "/?" + match.Groups[1].Value; break; case UrlFormat.Aspx: url = string.Concat("/", match.Groups[1].Value, ".aspx"); break; case UrlFormat.Html: url = string.Concat("/", match.Groups[1].Value, ".html"); break; default: url = "/" + match.Groups[1].Value; break; } } else { switch (settings.UrlFormat) { case UrlFormat.Query: url = string.Concat(root, "/?", match.Groups[1].Value); break; case UrlFormat.Aspx: url = string.Concat(root, "/", match.Groups[1].Value, ".aspx"); break; case UrlFormat.Html: url = string.Concat(root, "/", match.Groups[1].Value, ".html"); break; default: url = string.Concat(root, "/", match.Groups[1].Value); break; } } return(url); }
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); }
/// <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)); }
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 BuildAvatarVirtualPath(int userID, bool isUnapprovedAvatar, UserAvatarSize?size, string extendName) { string sizeString; if (size == null) { sizeString = "{0}"; } else { sizeString = GetAvatarSizeDirectoryName(size.Value); } if (isUnapprovedAvatar) { return(UrlUtil.JoinUrl(Globals.GetVirtualPath(SystemDirecotry.Upload_Avatar), Consts.User_UncheckAvatarSuffix, sizeString, GetAvatarLevel(userID, "/", extendName))); } else { return(Globals.GetPath(SystemDirecotry.Upload_Avatar, sizeString, GetAvatarLevel(userID, "/", extendName))); } }
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> /// 处理由头像Flash上传的已经截取好的图片(三个尺寸) /// </summary> /// <param name="Request"></param> public void SaveAvatar(AuthUser operatorUser, int targetUserID, HttpRequest request) { if (operatorUser == User.Guest) { ThrowError(new NotLoginError()); return; } if (operatorUser.UserID != targetUserID) { if (!CanEditUserAvatar(operatorUser, targetUserID)) { return; } } string tempFilename = request.QueryString["file"]; string extendName = Path.GetExtension(tempFilename).ToLower(); if (extendName != ".png" && extendName != ".gif" && extendName != ".jpg" && extendName != ".jpeg" && extendName != ".bmp") { throw new Exception("头像的文件扩展名不能是" + extendName); } uint length = 0; int lastIndex = 0; byte[] data = request.BinaryRead(request.TotalBytes); byte[] avatarData, dataLength, sizeArray; int sizeIndex = 0; UserAvatarSize avatarSize; dataLength = new byte[4]; sizeArray = StringUtil.Split <byte>(request.Headers["size"]); bool isUnappreved; //如果开启了头像审查,且操作者没有审查头像的权限,那么头像就应该是未审核状态 if (AllSettings.Current.AvatarSettings.EnableAvatarCheck && CanAvatarCheck(operatorUser) == false) { isUnappreved = true; } else { isUnappreved = false; } //同时上传3个尺寸的头像。 分割数据 while (lastIndex < data.Length) { dataLength[0] = data[lastIndex]; dataLength[1] = data[lastIndex + 1]; dataLength[2] = data[lastIndex + 2]; dataLength[3] = data[lastIndex + 3]; Array.Reverse(dataLength); length = BitConverter.ToUInt32(dataLength, 0); lastIndex += 4; avatarData = new byte[length]; for (int i = 0; i < length; i++) { avatarData[i] = data[lastIndex + i]; } lastIndex += (int)length; if (sizeArray[sizeIndex] == 24) { avatarSize = UserAvatarSize.Small; } else if (sizeArray[sizeIndex] == 120) { avatarSize = UserAvatarSize.Big; } else { avatarSize = UserAvatarSize.Default; } string savePath = BuildAvatarPath(targetUserID, isUnappreved, avatarSize, extendName); IOUtil.CreateDirectoryIfNotExists(Path.GetDirectoryName(savePath)); using (FileStream file = new FileStream(savePath, FileMode.Create)) { file.Write(avatarData, 0, avatarData.Length); } //UploadAvatar(operatorUser, targetUserID, temp, avatarType, extendName); sizeIndex++; } #region 对用户进行积分操作 if (isUnappreved) { string savePath = UrlUtil.JoinUrl(Globals.GetVirtualPath(SystemDirecotry.Upload_Avatar), Consts.User_UncheckAvatarSuffix, "{0}", GetAvatarLevel(targetUserID, "\\", extendName)); UserTempDataBO.Instance.SaveData(targetUserID, UserTempDataType.Avatar, savePath, true); } else { AuthUser user = GetAuthUser(targetUserID, true); string savePath = GetAvatarLevel(targetUserID, "/", extendName); user.AvatarPropFlag.OriginalData = savePath; UserDao.Instance.UpdateAvatar(targetUserID, user.AvatarPropFlag.GetStringForSave(), true); //RemoveUserCache(targetUserID); user.AvatarSrc = savePath; user.ClearAvatarCache(); if (OnUserAvatarChanged != null) { string smallAvatarPath = UrlUtil.JoinUrl(Globals.SiteRoot, user.SmallAvatarPath); string defaultAvatarPath = UrlUtil.JoinUrl(Globals.SiteRoot, user.AvatarPath); string bigAvatarPath = UrlUtil.JoinUrl(Globals.SiteRoot, user.BigAvatarPath); OnUserAvatarChanged(targetUserID, savePath, smallAvatarPath, defaultAvatarPath, bigAvatarPath); } } #endregion if (tempFilename.StartsWith(Globals.GetVirtualPath(SystemDirecotry.Temp_Avatar), StringComparison.OrdinalIgnoreCase)) { IOUtil.DeleteFile(tempFilename); //删除用户制作头像时上传的临时文件 } }