Пример #1
0
        /// <summary>
        /// 本地文件上传,秒传(根据lin_file表中的md5,与当前文件的路径是否在本地),如果不在,重新上传,覆盖文件表记录
        /// </summary>
        /// <param name="file"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <FileDto> UploadAsync(IFormFile file, int key = 0)
        {
            string  md5     = LinCmsUtils.GetHash <MD5>(file.OpenReadStream());
            LinFile linFile = await _fileRepository.Where(r => r.Md5 == md5 && r.Type == 1).OrderByDescending(r => r.CreateTime).FirstAsync();

            if (linFile != null && File.Exists(Path.Combine(_hostingEnv.WebRootPath, linFile.Path)))
            {
                return(new FileDto
                {
                    Id = linFile.Id,
                    Key = "file_" + key,
                    Path = linFile.Path,
                    Url = _fileStorageOption.LocalFile.Host + linFile.Path
                });
            }

            long id;

            var(path, len) = await this.LocalUploadAsync(file);

            if (linFile == null)
            {
                LinFile saveLinFile = new LinFile()
                {
                    Extension = Path.GetExtension(file.FileName),
                    Md5       = md5,
                    Name      = file.FileName,
                    Path      = path,
                    Type      = 1,
                    Size      = len
                };
                id = (await _fileRepository.InsertAsync(saveLinFile)).Id;
            }
            else
            {
                linFile.Path = path;
                await _fileRepository.UpdateAsync(linFile);

                id = linFile.Id;
            }

            return(new FileDto
            {
                Id = id,
                Key = "file_" + key,
                Path = path,
                Url = _fileStorageOption.LocalFile.Host + path
            });
        }
Пример #2
0
        /// <summary>
        /// 上传文件至七牛云
        /// </summary>
        /// <param name="file">单个文件</param>
        /// <param name="key"></param>
        /// <returns></returns>
        public FileDto Upload(IFormFile file, int key = 0)
        {
            string md5 = LinCmsUtils.GetHash <MD5>(file.OpenReadStream());

            LinFile linFile = _freeSql.Select <LinFile>().Where(r => r.Md5 == md5 && r.Type == 2).First();

            if (linFile != null)
            {
                return(new FileDto
                {
                    Id = linFile.Id,
                    Key = "file_" + key,
                    Path = linFile.Path,
                    Url = _configuration[LinConsts.Qiniu.Host] + linFile.Path
                });
            }

            string fileName = ContentDispositionHeaderValue
                              .Parse(file.ContentDisposition)
                              .FileName.Trim().ToString();

            string extension = Path.GetExtension(fileName);

            string path = this.UploadToQiniu(file);

            long    size        = 0;
            LinFile saveLinFile = new LinFile()
            {
                Extension  = extension,
                Md5        = md5,
                Name       = fileName,
                Path       = path,
                Type       = 2,
                CreateTime = DateTime.Now,
                Size       = size
            };

            long id = _freeSql.Insert(saveLinFile).ExecuteIdentity();

            return(new FileDto
            {
                Id = (int)id,
                Key = "file_" + key,
                Path = path,
                Url = _configuration[LinConsts.Qiniu.Host] + path
            });
        }
Пример #3
0
        /// <summary>
        /// 上传文件至七牛云,如果本地存在这条记录,直接返回文件的信息
        /// </summary>
        /// <param name="file">单个文件</param>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task <FileDto> UploadAsync(IFormFile file, int key = 0)
        {
            string md5 = LinCmsUtils.GetHash <MD5>(file.OpenReadStream());

            LinFile linFile = await _fileRepository.Where(r => r.Md5 == md5 && r.Type == 2).FirstAsync();

            if (linFile != null)
            {
                return(new FileDto
                {
                    Id = linFile.Id,
                    Key = "file_" + key,
                    Path = linFile.Path,
                    Url = _fileStorageOption.Qiniu.Host + linFile.Path
                });
            }

            string path = this.QiniuUpload(file);

            LinFile saveLinFile = new LinFile()
            {
                Extension = Path.GetExtension(file.FileName),
                Md5       = md5,
                Name      = file.FileName,
                Path      = path,
                Type      = 2,
                Size      = file.Length,
            };

            long id = (await _fileRepository.InsertAsync(saveLinFile)).Id;

            return(new FileDto
            {
                Id = id,
                Key = "file_" + key,
                Path = path,
                Url = _fileStorageOption.Qiniu.Host + path
            });
        }
        public FileDto Upload(IFormFile file, int key = 0)
        {
            string domainUrl = _configuration[LinConsts.SITE_DOMAIN];
            string fileDir   = _configuration[LinConsts.File.STORE_DIR];

            string md5 = LinCmsUtils.GetHash <MD5>(file.OpenReadStream());

            LinFile linFile = _freeSql.Select <LinFile>().Where(r => r.Md5 == md5 && r.Type == 1).OrderByDescending(r => r.CreateTime).First();

            if (linFile != null && File.Exists(Path.Combine(_hostingEnv.WebRootPath, fileDir, linFile.Path)))
            {
                return(new FileDto
                {
                    Id = linFile.Id,
                    Key = "file_" + key,
                    Path = linFile.Path,
                    Url = domainUrl + fileDir + "/" + linFile.Path
                });
            }

            string fileName  = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim().ToString();
            string extension = Path.GetExtension(fileName);
            string now       = DateTime.Now.ToString("yyy/MM/dd");

            string newSaveName = Guid.NewGuid() + extension;

            string savePath = Path.Combine(_hostingEnv.WebRootPath, fileDir, now);

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            long len;

            using (FileStream fs = File.Create(Path.Combine(savePath, newSaveName)))
            {
                file.CopyTo(fs);
                len = fs.Length;
                fs.Flush();
            }

            long   id;
            string path = Path.Combine(now, newSaveName).Replace("\\", "/");

            if (linFile == null)
            {
                LinFile saveLinFile = new LinFile()
                {
                    Extension  = Path.GetExtension(fileName),
                    Md5        = md5,
                    Name       = fileName,
                    Path       = path,
                    Type       = 1,
                    CreateTime = DateTime.Now,
                    Size       = len
                };
                id = _freeSql.Insert(saveLinFile).ExecuteIdentity();
            }
            else
            {
                _freeSql.Update <LinFile>(linFile.Id).Set(a => a.Path, path).ExecuteAffrows();
                id = linFile.Id;
            }

            return(new FileDto
            {
                Id = (int)id,
                Key = "file_" + key,
                Path = path,
                Url = domainUrl + "/" + fileDir + "/" + path
            });
        }
        public List <FileDto> Upload(IFormFile file, int key = 0)
        {
            string domainUrl = _configuration["SITE_DOMAIN"];
            string fileDir   = _configuration["FILE:STORE_DIR"];

            string md5 = LinCmsUtils.GetHash <MD5>(file.OpenReadStream());

            LinFile linFile = _freeSql.Select <LinFile>().Where(r => r.Md5 == md5).First();

            if (linFile != null)
            {
                return(new List <FileDto>
                {
                    new FileDto
                    {
                        Id = linFile.Id,
                        Key = "file_" + key,
                        Path = linFile.Path,
                        Url = domainUrl + "/" + _configuration["FILE:STORE_DIR"] + "/" + linFile.Path
                    }
                });
            }

            string filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim().ToString();

            DateTime now = DateTime.Now;

            string newSaveName = Guid.NewGuid() + Path.GetExtension(filename);

            string savePath = Path.Combine(_hostingEnv.WebRootPath, fileDir, now.ToString("yyy/MM/dd"));

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            int len;

            using (FileStream fs = System.IO.File.Create(Path.Combine(savePath, newSaveName)))
            {
                file.CopyTo(fs);
                len = (int)fs.Length;
                fs.Flush();
            }

            LinFile saveLinFile = new LinFile()
            {
                Extension  = Path.GetExtension(filename),
                Md5        = md5,
                Name       = filename,
                Path       = Path.Combine(now.ToString("yyy/MM/dd"), newSaveName).Replace("\\", "/"),
                Type       = 1,
                CreateTime = DateTime.Now,
                Size       = len
            };

            long id = _freeSql.Insert(saveLinFile).ExecuteIdentity();

            return(new List <FileDto>
            {
                new FileDto
                {
                    Id = (int)id,
                    Key = "file_" + key,
                    Path = saveLinFile.Path,
                    Url = domainUrl + "/" + fileDir + "/" + saveLinFile.Path
                }
            });
        }