/// <summary> /// 映射 /// </summary> /// <param name="a"></param> /// <returns></returns> public ResourceFileInfo ToResourceFileInfo(Base_CatalogArticle a) { //return ResourceCatalogInit.ToResourceFileInfo(a); ResourceFileInfo info = new ResourceFileInfo(); info.Id = a.Id; info.Keywords = a.Article.Keywords; info.CreateTime = a.Article.CreateTime; info.Abstract = a.Article.Abstract; info.UserId = a.Article.EditorId.ToString(); info.CatalogId = a.CatalogId; info.ArticleId = a.ArticleId; //获取扩展属性 var fileKeyId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.Key); var MD5CodeId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.MD5Code); var contentTypeId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.ContentType); var fileNameId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.FileName); var fileSizeId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.FileSize); info.FileKey = a.Article.Exts.FirstOrDefault(t => t.CatlogExtId == fileKeyId).Value; info.MD5Code = a.Article.Exts.FirstOrDefault(t => t.CatlogExtId == MD5CodeId).Value; info.ContentType = a.Article.Exts.FirstOrDefault(t => t.CatlogExtId == contentTypeId).Value; info.FileName = a.Article.Exts.FirstOrDefault(t => t.CatlogExtId == fileNameId).Value; info.FileSize = long.Parse(a.Article.Exts.FirstOrDefault(t => t.CatlogExtId == fileSizeId).Value); return(info); }
/// <summary> /// 当选择系统默认头像时,保存头像 /// </summary> /// <param name="info"></param> public void SaveAvatar(ResourceFileInfo info) { var myArticle = SiteManager.Kernel.Get <EFAuditDataService <Base_Article> >(); Base_Article art = new Base_Article(); art = myArticle.GetQuery().Include(a => a.Exts).FirstOrDefault(a => a.Id == info.Id); Base_Article newart = new Base_Article { EditorId = Convert.ToInt32(info.UserId), Author = info.UserId, Title = info.UserId + "的头像", State = ArticleState.ReadOnly }; var avatarCatId = Catalog.GetAllValid().First(cat => cat.Name == "UserAvatar" && cat.ParentId == Catalog.GetRootId()).Id; Base_CatalogArticle catArt = new Base_CatalogArticle { Article = newart, CatalogId = avatarCatId, ArticleId = newart.Id, }; var rootId = Catalog.GetRootId(); catArt.SetExt(SiteManager.Catalog.GetExtByName(rootId, SystemTypes.Root.Key), art.Exts.FirstOrDefault(e => e.CatlogExtId == SiteManager.Catalog.GetExtByName(rootId, SystemTypes.Root.Key).Id).Value); catArt.SetExt(SiteManager.Catalog.GetExtByName(rootId, SystemTypes.Root.FileName), art.Exts.FirstOrDefault(e => e.CatlogExtId == SiteManager.Catalog.GetExtByName(rootId, SystemTypes.Root.FileName).Id).Value); catArt.SetExt(SiteManager.Catalog.GetExtByName(rootId, SystemTypes.Root.FileSize), art.Exts.FirstOrDefault(e => e.CatlogExtId == SiteManager.Catalog.GetExtByName(rootId, SystemTypes.Root.FileSize).Id).Value); _article.Save(catArt); _article.Dispose(); }
/// <summary> /// 根据名称和大小获取文件信息 /// </summary> /// <param name="size">大小</param> /// <param name="fileName">名称</param> /// <returns>文件信息</returns> public ResourceFileInfo GetFileBySizeName(long size, string fileName, bool getStream = false) { var sizeStr = size.ToString(); var fileNameId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.FileName); var fileSizeId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.FileSize); var a = _article.GetQuery().FirstOrDefault( t => t.Article.Exts.Any(ext => ext.CatlogExtId == fileNameId && ext.Value.Equals(fileName)) && (t.Article.Exts.Any(ext => ext.CatlogExtId == fileSizeId && ext.Value == sizeStr))); if (a == null) { return(null); } ResourceFileInfo info = ToResourceFileInfo(a); if (getStream) { info.FileStream = StreamCache[info.FileKey]; } return(info); }
/// <summary> /// 保存上传记录,其中info.Id不能为空 /// </summary> /// <param name="info"></param> public Base_CatalogArticle SaveCatalogArticleInfo(ResourceFileInfo info) { if (info.Id == 0) { return(null); } var a = _article.GetById(info.Id); //当上传的目录Id为0时表示上传到默认(根目录) if (info.CatalogId == 0) { info.CatalogId = GetUserRootCatalog(a.Article.EditorId).Id; } //判断是否在相同的文件夹上传重复的文件 if (_article.GetAllQuery().Where(art => art.ArticleId == a.ArticleId).Select(cat => cat.CatalogId).Contains(info.CatalogId)) { int id = _article.GetAllQuery().FirstOrDefault(art => art.ArticleId == a.ArticleId && art.CatalogId == info.CatalogId).Id; _article.Delete(id); } Base_CatalogArticle ua = new Base_CatalogArticle { Article = a.Article, ArticleId = a.ArticleId, CatalogId = info.CatalogId, CreateTime = DateTime.Now }; _article.Save(ua); return(ua); }
private ResourceFileInfo InnerSavePartialFile(ResourceFileInfo info) { ResourceFileInfo savedInfo = new ResourceFileInfo(); if (info.StartPos == 0) { //新文件,第一部分流 //保存文件属性信息 savedInfo = InnerSaveArticleInfo(info); //保存文件流 StreamCache[savedInfo.FileKey] = info.FileStream; } else { //后续流 savedInfo = GetFileBySizeName(info.FileSize, info.FileName); info.Id = savedInfo.Id; //保存文件流 StreamCache.Append(savedInfo.FileKey, info.FileStream, info.StartPos); } ChangeArtState(info); return(savedInfo); }
/// <summary> /// 获得用户的当前头像 /// </summary> /// <param name="userId">用户的Id</param> /// <returns></returns> public ResourceFileInfo GetUserAvatar(int userId) { ResourceFileInfo info; //获取用户头像目录ID var avatarCatId = Catalog.GetAllValid().First(cat => cat.Name == "UserAvatar" && cat.ParentId == Catalog.GetRootId()).Id; string uid = userId.ToString(); var catArt = _article.GetQuery().Where(c => c.CatalogId == avatarCatId).OrderByDescending(c => c.Id).FirstOrDefault(c => c.Article.Author == uid); //如果用户没有选择过头像,就给一个默认的头像 if (catArt == null) { info = GetAllSysAvatar().First(); SaveAvatar(new ResourceFileInfo { Id = info.Id, UserId = userId.ToString(), }); } else { info = new ResourceFileInfo { //FileStream = GetFileThumbnail(catArt.Id, ThumbnailSize.Small), Id = catArt.Article.Id, ContentType = Convert.ToInt32(catArt.Article.State).ToString() }; } return(info); }
private ResourceFileInfo InnerSaveWholeFile(ResourceFileInfo info) { //保存文件属性信息 ResourceFileInfo savedInfo = InnerSaveArticleInfo(info); //保存文件流 StreamCache[savedInfo.FileKey] = info.FileStream; ChangeArtState(info); return(savedInfo); }
/// <summary> /// 获取文件流,不存在返回null,存在返回文件流Stream /// </summary> /// <param name="fileId">文件信息ID</param> /// <returns></returns> public Stream GetFileStream(int fileId) { var a = _article.GetById(fileId); if (a == null) { return(null); } ResourceFileInfo info = ToResourceFileInfo(a); return(StreamCache[info.FileKey]); }
public ResourceFileInfo SaveArticleInfo(ResourceFileInfo info) { var fileNameId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.FileName); var a = _article.GetQuery().FirstOrDefault( t => t.Article.Exts.Any(ext => ext.CatlogExtId == fileNameId && ext.Value.Equals(info.FileName))); a.Article.Keywords = info.Keywords; a.Article.EditTime = info.CreateTime; a.Article.Abstract = info.Abstract; _article.Save(a); return(info); }
/// <summary> /// 获取文件信息(包括文件流),不存在返回null /// </summary> /// <param name="ca"></param> /// <returns></returns> public ResourceFileInfo GetFile(Base_CatalogArticle ca) { if (ca == null) { return(null); } ResourceFileInfo info = ToResourceFileInfo(ca); info.FileStream = StreamCache[info.FileKey]; return(info); }
/// <summary> /// 当文件上传完整之后修改文件的状态 /// </summary> /// <param name="info"></param> private void ChangeArtState(ResourceFileInfo info) { if (info.Id == 0) { return; } if (info.EndPos == info.FileSize - 1 || info.EndPos == 0) { var a = _article.GetById(info.Id); a.Article.State = ArticleState.Published; _article.Save(a); } }
/// <summary> /// 保存文件信息 /// </summary> /// <param name="info">文件信息</param> public ResourceFileInfo SaveFile(ResourceFileInfo info) { ResourceFileInfo savedInfo = null; if (info.EndPos > 0) { //分块文件流 savedInfo = InnerSavePartialFile(info); } else { //整个文件流 savedInfo = InnerSaveWholeFile(info); } return(savedInfo); }
/// <summary> /// 获取文件的目录 /// </summary> /// <param name="fileId">文件ID</param> /// <returns></returns> public ResourceCatalogInfo GetCatalogOfFile(int fileId) { var a = _article.GetById(fileId); if (a == null) { return(null); } ResourceFileInfo info = ToResourceFileInfo(a); int catalogId = info.CatalogId; Base_Catalog c = Catalog.GetById(catalogId); var rc = ToResourceCatalogInfo(c); return(rc); }
/// <summary> /// 保存文件属性信息 /// </summary> /// <param name="info"></param> /// <returns></returns> private ResourceFileInfo InnerSaveArticleInfo(ResourceFileInfo info) { //string fileKey = Guid.NewGuid().ToString(); //文件Key,作为物理文件的文件名称,8位日期 + 年月日时分秒毫秒 by_zjf + 实际的上传文件名称 string fileKey = DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-" + info.FileName; int catId = GetCatalogId(info.ContentType); //保存文件属性信息 var a = _article.CreateByCatalog(catId); a.CreateTime = DateTime.Now; a.Article.State = ArticleState.Incomplete; a.Article.Title = info.FileName; a.Article.EditorId = int.Parse((info.UserId)); a.Article.Author = info.UserId; a.Article.Text = ""; a.Article.Keywords = info.Keywords; a.Article.EditTime = info.CreateTime; a.Article.Abstract = info.Abstract; //查找扩展属性ID,对应ID赋值 a.SetExt(SystemTypes.Root.Key, fileKey); a.SetExt(SystemTypes.Root.MD5Code, info.MD5Code); a.SetExt(SystemTypes.Root.ContentType, info.ContentType); a.SetExt(SystemTypes.Root.FileName, info.FileName); a.SetExt(SystemTypes.Root.FileSize, info.FileSize); SaveExtraExt(a, info); //保存文件信息到系统目录 _article.Save(a); info.Id = a.Id; var saved = SaveCatalogArticleInfo(info); info.Id = saved.Id; info.FileKey = fileKey; return(info); }
/// <summary> /// 获取文件信息(不包括文件流),不存在返回null /// </summary> /// <param name="size">文件大小</param> /// <param name="fileMD5">MD5码</param> /// <returns></returns> public ResourceFileInfo GetFile(long size, string fileMD5) { var sizeStr = size.ToString(); var MD5CodeId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.FileName); var fileSizeId = Catalog.GetExtId(SystemTypes.Root.Id, SystemTypes.Root.FileName); var a = _article.GetAllQuery().FirstOrDefault( t => t.Article.Exts.Any(ext => ext.CatlogExtId == MD5CodeId && ext.Value.Equals(fileMD5)) && (t.Article.Exts.Any(ext => ext.CatlogExtId == fileSizeId && ext.Value == sizeStr))); if (a == null) { return(null); } ResourceFileInfo info = ToResourceFileInfo(a); return(info); }
private void SaveExtraExt(Base_CatalogArticle art, ResourceFileInfo info) { //TODO: 根据文件类型保存额外的扩展属性 }