public IActionResult Delete(long id) { var user = _UserManager.Get(id); var userRole = user.UserTypeUser.OrderBy(it => it.UserType.Priority).Last(); UserType maxCurrentUserType = UserType.GetMaxUserType((User.FindFirstValue(ClaimTypes.Role) ?? "").Split(",")); if (user != null) { if (user.Username == User.FindFirstValue(ClaimTypes.NameIdentifier)) { return(Json(new { success = false, responseText = "You cannot remove yourself!" })); } else if (userRole != null && maxCurrentUserType != null && UserType.CompareRole(maxCurrentUserType.UserTypeName, userRole.UserType.UserTypeName) < 0) { return(Json(new { success = false, responseText = "You do not have sufficient authority to delete this account!" })); } else { _UserManager.Delete(user); user.HashPassword = ""; var uploads = Path.Combine(host.GetContentPathRootForUploadUtils(), NameUtils.ControllerName <UploadsController>().ToLower(), user.Username.ToLower()); // Xóa thư mục tệp tin của người dùng này nếu có tồn tại if (Directory.Exists(uploads)) { Directory.Delete(uploads, true); } return(Json(new { success = true, user = JsonConvert.SerializeObject(user), responseText = "Deleted" })); } } else { return(Json(new { success = false, responseText = "Can not find this user!" })); } }
public static void RemoveUploadMeida(this IHostEnvironment _host, string mediaPath) { if (mediaPath != null && mediaPath.Length > 0) { var destination = Path.Combine(_host.GetContentPathRootForUploadUtils(), mediaPath.TrimStart(Path.DirectorySeparatorChar)); if (File.Exists(destination)) { File.Delete(destination); } } }
public static async Task <string> UploadImage(this IHostEnvironment _host, IFormFile file, params string[] subFolders) { if (file != null && file.Length > 0 && file.Length <= Config.MAX_IMAGE_SIZE) { if (MimeTypeUtils.Image.CheckContentType(file.ContentType) && MimeTypeUtils.Image.CheckFileExtension(file.FileName)) { // Upload avatar try { var uniqueFileName = NameUtils.GetUniqueFileName(file.FileName); var uploads = Path.Combine(_host.GetContentPathRootForUploadUtils(), NameUtils.ControllerName <UploadsController>().ToLower()); foreach (string subFolder in subFolders) { uploads = Path.Combine(uploads, subFolder); } uploads = Path.Combine(uploads, "images"); // Kiểm tra xem folder có tồn tại không? Nếu không thì tạo mới if (!Directory.Exists(uploads)) { Directory.CreateDirectory(uploads); } var filePath = Path.Combine(uploads, uniqueFileName); using (var stream = File.Create(filePath)) { await file.CopyToAsync(stream); } return(filePath.Replace(_host.GetContentPathRootForUploadUtils(), "")); } catch (Exception) { } } } return(""); }
// Hàm tạo component public IViewComponentResult Invoke() { string root = host.GetContentPathRootForUploadUtils(); // Tạo thư mục mặc định cho speaking_embed var speakingEmbedFolder = Path.Combine(root, UploadsController.MANAGER_ROOT_DIRECTORY, UploadsController.VIDEOS_DIRECTORY); // Nếu thư mục chưa tồn tại, thì trả về trống if (!Directory.Exists(speakingEmbedFolder)) { return(View(new List <LibraryVideo>())); } return(View(DirSearch(speakingEmbedFolder))); }
public static async Task <string> UploadForUserImage(this IHostEnvironment _host, IFormFile file, User user) { string res = await UploadImage(_host, file, user.Username.ToLower()); if (res != null && res.Length > 0) { // Xóa tệp ảnh cũ nếu có if (user.Avatar != null && user.Avatar.Length > 0) { var oldFile = Path.Combine(_host.GetContentPathRootForUploadUtils(), user.Avatar.TrimStart(Path.DirectorySeparatorChar)); if (File.Exists(oldFile)) { File.Delete(oldFile); } } return(res); } else { return(""); } }