/// <summary> /// modify the user avatar /// </summary> /// <returns>(bool: isSuccessfully, string: message when fault or avatar request path when successfully)</returns> public virtual async Task <(bool, string)> ModifyAvatarAsync(int avatarId) { await using var db = new LOPDbContext(); DB.Tables.User user = await db.Users.AsNoTracking().Include(user => user.Avatar).FirstOrDefaultAsync(user => user.Id == Id); if (user is null) { return(false, "该用户不存在"); } if (user.AvatarId == avatarId) { return(true, ""); } DB.Tables.File SOURCE_AVATAR = user.Avatar; int shouldChangeCount = 2; // 删除原头像 if (user.AvatarId != AVATAR_DEFAULT_ID && user.Avatar != null) { db.Files.Remove(SOURCE_AVATAR); shouldChangeCount++; } // user.AvatarId = avatarId; var avatarModel = await db.Files.AsNoTracking().FirstOrDefaultAsync(file => file.Id == avatarId); if (avatarModel == null) { return(false, "该头像不存在"); } user.Avatar = avatarModel; db.Users.Update(user); int changeCount = await db.SaveChangesAsync(); if (changeCount == shouldChangeCount) { if (SOURCE_AVATAR.Id != AVATAR_DEFAULT_ID) { // 删除原头像文件 Files.File.Delete(SOURCE_AVATAR.SaveName); // 删除缩略图 Files.File.DeleteThumbnail(SOURCE_AVATAR.Thumbnail); } // 缓存用户更新后的数据 UserCache.SetUserModel(user); // 返回新头像的访问路径 string saveWebPath = Config.GetValue("File:SaveWebPath"); saveWebPath = Path.Combine(saveWebPath, avatarModel.SaveName); return(true, saveWebPath); } throw new Exception("修改头像失败"); }
public File(DB.Tables.File file) { if (file is null) { throw new ArgumentNullException(); } Id = file.Id; Name = file.Name; ExtensionName = file.ExtensionName; Size = file.Size; SourcePath = file.Path; ThumbnailPath = file.Thumbnail; }
/// <summary> /// save file info to DB /// </summary> /// <param name="fileInfo">file info</param> /// <returns>id of file in DB</returns> public static async Task <(bool, int)> SaveToDBAsync(string fileName, string saveName, long size) { await using var db = new LOPDbContext(); DB.Tables.File newFileModel = new DB.Tables.File { Name = fileName, SaveName = saveName, Extension = Path.GetExtension(fileName), Size = size }; await db.AddAsync(newFileModel); bool success = await db.SaveChangesAsync() == 1; return(success, success ? newFileModel.Id : -1); }
/// <summary> /// 获取一张图片的缩略图路径和原图路径 /// </summary> /// <param name="id"></param> /// <returns></returns> public static Share.Image GetImagePath(int id) { using var db = new DB.DarkContext(); DB.Tables.File file = db.Files.AsNoTracking() .FirstOrDefault(f => f.Id == id); Share.Image image = new Share.Image { Thumbnail = "", Source = "" }; if (file is null) { return(image); } image.Thumbnail = file.Thumbnail; image.Source = file.Path; return(image); }
/// <summary> /// 获取默认头像的ID /// </summary> /// <returns>返回 id, 没有的话会返回 NOT_FILES</returns> public static int GetDefaultAvatarId() { const string KEY = "d8154a54-c1ef-4ed2-8f91-66bdf6aa8f80"; string value = Cache.Get <string>(KEY); if (value != null && int.TryParse(value, out int id)) { return(id); } using var db = new MyForContext(); DB.Tables.File file = db.Files.AsNoTracking().FirstOrDefault(f => f.Path == DEFAULT_AVATAR); if (file is null) { return(0); } _ = Cache.Set(KEY, file.Id, 10); return(file.Id); }
/// <summary> /// 保存图片, 返回图片信息 /// </summary> /// <param name="file"></param> /// <returns></returns> public static async Task <File> SaveImageAsync(IFormFile file, double thumbnailWidth = THUMBNAIL_WIDTH, double thumbnailHeight = THUMBNAIL_HEIGHT) { /* * 保存图片到数据库, 同时保存图片的缩略图 */ if (file is null) { return(null); } // 源文件名 string fileName = file.FileName; // 扩展名 string ExtensionName = Path.GetExtension(fileName); // 保存名 string saveName = string.Concat(Guid.NewGuid().ToString(), ExtensionName); // 源文件大小 long size = file.Length; // 缩略图名字 // 原图保存名 + '-min' + 缩略图扩展名 string thumbnailName = saveName.Replace(ExtensionName, $"-min{DEFAULT_THUMBNAIL_EXTENSIONS}"); // 缩略图保存路径 string thumbnailPath = Path.Combine(SaveThumbnailPath, thumbnailName); // 原图的保存路径 string sourceSavePath = Path.Combine(SavePath, saveName); // 保存原图 using (Stream stream = System.IO.File.Create(sourceSavePath)) { await file.CopyToAsync(stream); } string coverTempName = saveName.Replace(ExtensionName, $"-cover{DEFAULT_THUMBNAIL_EXTENSIONS}"); string coverTempPath = null; // 制作缩略图的源文件 // 静态图是源文件 // GIF 图是截取的封面 string thumbnailSourcePath; // 是否 GIF // 是的话, 保存 GIF 的封面的缩略图 if (ExtensionName.ToUpper() == GIF) { /* * 先截取封面所临时缩略图 * 再使用里临时缩略图制作缩略图 */ coverTempPath = Path.Combine(SaveTempPath, coverTempName); GetGIFCover(sourceSavePath, coverTempPath); thumbnailSourcePath = coverTempPath; } else { thumbnailSourcePath = sourceSavePath; } // 保存缩略图 Task thumbnailT = MakeThumbnail(thumbnailSourcePath, thumbnailPath, thumbnailWidth, thumbnailHeight); DB.Tables.File fileModel = new DB.Tables.File { Name = fileName, ExtensionName = ExtensionName, Size = size, Path = Path.Combine(WebSaveDirectory, saveName), Thumbnail = Path.Combine(WebSaveThumbnailDirectory, thumbnailName) }; using DB.DarkContext db = new DB.DarkContext(); db.Files.Add(fileModel); int suc = await db.SaveChangesAsync(); // 等待缩略图保存完成 await thumbnailT; // 删除临时文件 if (!string.IsNullOrWhiteSpace(coverTempPath)) { System.IO.File.Delete(coverTempPath); } if (suc == 1) { return(new File(fileModel)); } return(null); }