コード例 #1
0
 public NoteFileService(DataContext dataContext, ConfigFileService ConfigFileService)
 {
     this.dataContext       = dataContext;
     this.ConfigFileService = ConfigFileService;
     this.storeConfig       = ConfigFileService.WebConfig.FileStoreConfig;
     this.webSiteConfig     = ConfigFileService.WebConfig;
 }
コード例 #2
0
 public EntryArtistImageProvider(IFileRepository <NamedPicture> pictureRepository,
                                 FileStoreConfig fileStoreConfig,
                                 PathToUrlConverterConfig pathToUrlConverterConfig)
 {
     this.pictureRepository        = pictureRepository;
     this.fileStoreConfig          = fileStoreConfig;
     this.pathToUrlConverterConfig = pathToUrlConverterConfig;
 }
コード例 #3
0
 public AdminService(
     ILogger <AdminService> logger,
     LastFmConfig lastFmConfig,
     FileStoreConfig fileStoreConfig,
     PathToUrlConverterConfig pathToUrlConverterConfig,
     IRepository <TopItem> repository)
 {
     this.logger                   = logger;
     this.lastFmConfig             = lastFmConfig;
     this.fileStoreConfig          = fileStoreConfig;
     this.pathToUrlConverterConfig = pathToUrlConverterConfig;
     this.repository               = repository;
 }
コード例 #4
0
ファイル: BaseController.cs プロジェクト: hyfree/MoreNote
        protected string UploadImagesOrAttach(ref FileModel fileModel, out string msg, long?userId)
        {
            if (fileModel == null)
            {
                msg = "fileModel=null";
                return(null);
            }
            //检查哈登录
            msg = string.Empty;


            if (userId == null)
            {
                msg = "Need to log in";
                return(null);
            }

            if (string.IsNullOrEmpty(fileModel.fileName) || fileModel.fileName.IndexOf(".") == -1)
            {
                fileModel.fileName = "unknow.jpg";
            }
            if (fileModel.data == null || fileModel.data.Length == 0)
            {
                return(null);
            }
            WebSiteConfig   webSiteConfig   = configFileService.WebConfig;
            FileStoreConfig fileStoreConfig = configFileService.WebConfig.FileStoreConfig;
            string          uploadDirPath   = null;

            var diskFileId = idGenerator.NextId();

            string uploadType   = "images";//images  attachments
            string datafileName = fileModel.fileName;

            if (datafileName.IndexOf("&") > 1)
            {
                datafileName = datafileName.Substring(0, datafileName.IndexOf("&"));
            }

            var fileEXT = Path.GetExtension(datafileName).Replace(".", "");

            var ext = Path.GetExtension(datafileName);

            if (IsAllowImageExt(fileEXT))
            {
                //msg = $"The_image_extension_{fileEXT}_is_blocked";
                //return null;
                uploadType = "images";
            }
            else if (IsAllowAttachExt(fileEXT))
            {
                uploadType = "attachments";
            }
            else
            {
                uploadType = "images";
                fileEXT    = "png";
                ext        = ".png";
            }
            var    fileName  = diskFileId.ToHex() + "." + fileEXT;
            string resultURL = string.Empty;//最终返回URL

            //将文件保存在磁盘
            //Task<bool> task = noteFileService.SaveUploadFileOnDiskAsync(httpFile, uploadDirPath, fileName);
            try
            {
                var  provider   = new FileExtensionContentTypeProvider();
                var  memi       = provider.Mappings[ext];
                var  nowTime    = DateTime.Now;
                var  objectName = $"{userId.ToHex()}/{uploadType}/{ nowTime.ToString("yyyy")}/{nowTime.ToString("MM")}/{diskFileId.ToHex()}{ext}";
                bool result     = noteFileService.SaveFile(objectName, fileModel.data, memi).Result;

                if (result)
                {
                    //将结果保存在数据库
                    NoteFile noteFile = new NoteFile()
                    {
                        FileId      = diskFileId,
                        UserId      = userId,
                        AlbumId     = 1,
                        Name        = fileName,
                        Title       = fileName,
                        Path        = uploadDirPath + fileName,
                        Size        = fileModel.data.Length,
                        CreatedTime = nowTime
                                      //todo: 增加特性=图片管理
                    };
                    var AddResult = noteFileService.AddImage(noteFile, 0, userId, true);
                    if (!AddResult)
                    {
                        msg = "添加数据库失败";
                        return(null);
                    }
                    else
                    {
                        resultURL = webSiteConfig.APPConfig.SiteUrl + "/CacheServer/File/Images/" + diskFileId.ToHex();
                    }
                }
                else
                {
                    msg = "磁盘保存失败";

                    return(null);
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                return(null);
            }
            msg = "success";
            return(resultURL);
        }
コード例 #5
0
ファイル: BaseController.cs プロジェクト: hyfree/MoreNote
        protected UploadData UploadImagesOrAttach(long?userId, out string msg)
        {
            //检查哈登录
            msg = string.Empty;
            if (userId == null)
            {
                msg = "Need to log in";
                return(null);
            }

            FileStoreConfig config        = configFileService.WebConfig.FileStoreConfig;
            string          uploadDirPath = null;

            var diskFileId = idGenerator.NextId();

            var httpFiles = _accessor.HttpContext.Request.Form.Files;

            if (httpFiles == null || httpFiles.Count < 1)
            {
                msg = "Invalid upload";
                return(null);
            }
            var uploadData = new UploadData();

            foreach (var httpFile in httpFiles)
            {
                string uploadType = "images";//images  attachments
                var    fileEXT    = Path.GetExtension(httpFile.FileName).Replace(".", "");
                var    ext        = Path.GetExtension(httpFile.FileName);
                if (IsAllowImageExt(fileEXT))
                {
                    //msg = $"The_image_extension_{fileEXT}_is_blocked";
                    //return null;
                    uploadType = "images";
                }
                else if (IsAllowAttachExt(fileEXT))
                {
                    uploadType = "attachments";
                }
                else
                {
                    uploadData.errFiles.Add(httpFile.FileName);
                    continue;
                }
                var fileName = diskFileId.ToHex() + "." + fileEXT;

                //将文件保存在磁盘
                //Task<bool> task = noteFileService.SaveUploadFileOnDiskAsync(httpFile, uploadDirPath, fileName);
                try
                {
                    var  provider   = new FileExtensionContentTypeProvider();
                    var  memi       = provider.Mappings[ext];
                    var  nowTime    = DateTime.Now;
                    var  objectName = $"{userId.ToHex()}/{uploadType}/{ nowTime.ToString("yyyy")}/{nowTime.ToString("MM")}/{diskFileId.ToHex()}{ext}";
                    bool result     = noteFileService.SaveFile(objectName, httpFile, memi).Result;

                    if (result)
                    {
                        //将结果保存在数据库
                        NoteFile noteFile = new NoteFile()
                        {
                            FileId      = diskFileId,
                            UserId      = userId,
                            AlbumId     = 1,
                            Name        = fileName,
                            Title       = fileName,
                            Path        = uploadDirPath + fileName,
                            Size        = httpFile.Length,
                            CreatedTime = nowTime
                                          //todo: 增加特性=图片管理
                        };
                        var AddResult = noteFileService.AddImage(noteFile, 0, userId, true);
                        if (!AddResult)
                        {
                            msg = "添加数据库失败";
                            uploadData.errFiles.Add(httpFile.FileName);
                            continue;
                        }
                        else
                        {
                            uploadData.succMap.Add(httpFile.FileName, "/CacheServer/File/Images/" + diskFileId.ToHex());
                        }
                    }
                    else
                    {
                        msg = "磁盘保存失败";
                        uploadData.errFiles.Add(httpFile.FileName);
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    uploadData.errFiles.Add(httpFile.FileName);
                    continue;
                }
            }
            msg = "success";
            return(uploadData);
        }
コード例 #6
0
ファイル: BaseController.cs プロジェクト: hyfree/MoreNote
        protected bool UploadImages(string name, long?userId, long?noteId, bool isAttach, out long?serverFileId, out string msg)
        {
            if (isAttach)
            {
                return(UploadAttach(name, userId, noteId, out msg, out serverFileId));
            }
            msg          = "";
            serverFileId = 0;
            FileStoreConfig config        = configFileService.WebConfig.FileStoreConfig;
            string          uploadDirPath = null;

            var diskFileId = idGenerator.NextId();

            serverFileId = diskFileId;
            var httpFiles = _accessor.HttpContext.Request.Form.Files;

            //检查是否登录
            if (userId == 0)
            {
                userId = GetUserIdBySession();
                if (userId == 0)
                {
                    msg = "NoLogin";
                    return(false);
                }
            }

            if (httpFiles == null || httpFiles.Count < 1)
            {
                return(false);
            }
            var httpFile = httpFiles[name];
            var fileEXT  = Path.GetExtension(httpFile.FileName).Replace(".", "");
            var ext      = Path.GetExtension(httpFile.FileName);

            if (!IsAllowImageExt(fileEXT))
            {
                msg = $"The_image_extension_{fileEXT}_is_blocked";
                return(false);
            }
            var fileName = diskFileId.ToHex() + "." + fileEXT;

            //判断合法性
            if (httpFiles == null || httpFile.Length < 0)
            {
                return(false);
            }
            //将文件保存在磁盘
            //Task<bool> task = noteFileService.SaveUploadFileOnDiskAsync(httpFile, uploadDirPath, fileName);

            var  provider   = new FileExtensionContentTypeProvider();
            var  memi       = provider.Mappings[ext];
            var  nowTime    = DateTime.Now;
            var  objectName = $"{userId.ToHex()}/images/{ nowTime.ToString("yyyy")}/{nowTime.ToString("MM")}/{diskFileId.ToHex()}{ext}";
            bool result     = noteFileService.SaveFile(objectName, httpFile, memi).Result;

            if (result)
            {
                //将结果保存在数据库
                NoteFile noteFile = new NoteFile()
                {
                    FileId      = diskFileId,
                    UserId      = userId,
                    AlbumId     = 1,
                    Name        = fileName,
                    Title       = fileName,
                    Path        = uploadDirPath + fileName,
                    Size        = httpFile.Length,
                    CreatedTime = nowTime
                                  //todo: 增加特性=图片管理
                };
                var AddResult = noteFileService.AddImage(noteFile, 0, userId, true);
                if (!AddResult)
                {
                    msg = "添加数据库失败";
                }
                return(AddResult);
            }
            else
            {
                msg = "磁盘保存失败";
                return(false);
            }
        }
コード例 #7
0
ファイル: BaseController.cs プロジェクト: hyfree/MoreNote
        protected bool UploadAttach(string name, long?userId, long?noteId, out string msg, out long?serverFileId)
        {
            msg          = "";
            serverFileId = 0;
            FileStoreConfig config = configFileService.WebConfig.FileStoreConfig;

            var diskFileId = idGenerator.NextId();

            serverFileId = diskFileId;
            var httpFiles = _accessor.HttpContext.Request.Form.Files;

            //检查是否登录
            if (userId == 0)
            {
                userId = GetUserIdBySession();
                if (userId == 0)
                {
                    msg = "NoLogin";
                    return(false);
                }
            }

            if (httpFiles == null || httpFiles.Count < 1)
            {
                return(false);
            }
            var httpFile = httpFiles[name];
            var fileEXT  = Path.GetExtension(httpFile.FileName).Replace(".", "");

            if (!IsAllowAttachExt(fileEXT))
            {
                msg = $"The_Attach_extension_{fileEXT}_is_blocked";
                return(false);
            }
            var fileName = diskFileId.ToHex() + "." + fileEXT;

            //判断合法性
            if (httpFiles == null || httpFile.Length < 0)
            {
                return(false);
            }
            //将文件保存在磁盘
            // Task<bool> task = noteFileService.SaveUploadFileOnUPYunAsync(upyun, httpFile, uploadDirPath, fileName);
            //Task<bool> task = noteFileService.SaveUploadFileOnDiskAsync(httpFile, uploadDirPath, fileName);

            var  ext        = Path.GetExtension(fileName);
            var  provider   = new FileExtensionContentTypeProvider();
            var  memi       = provider.Mappings[ext];
            var  nowTime    = DateTime.Now;
            var  objectName = $"{userId.ToHex()}/attachments/{ nowTime.ToString("yyyy")}/{nowTime.ToString("MM")}/{diskFileId.ToHex()}{ext}";
            bool result     = noteFileService.SaveFile(objectName, httpFile, memi).Result;

            if (result)
            {
                //将结果保存在数据库
                AttachInfo attachInfo = new AttachInfo()
                {
                    AttachId     = diskFileId,
                    UserId       = userId,
                    NoteId       = noteId,
                    UploadUserId = userId,
                    Name         = fileName,
                    Title        = httpFile.FileName,
                    Size         = httpFile.Length,
                    Path         = fileName,
                    Type         = fileEXT.ToLower(),
                    CreatedTime  = DateTime.Now
                                   //todo: 增加特性=图片管理
                };
                var AddResult = attachService.AddAttach(attachInfo, true, out string AttachMsg);
                if (!AddResult)
                {
                    msg = "添加数据库失败";
                }
                return(AddResult);
            }
            else
            {
                msg = "磁盘保存失败";
                return(false);
            }
        }
コード例 #8
0
 public FileManager(IOptions <FileStoreConfig> configuration)
 {
     _configuration = configuration.Value;
 }