Example #1
0
        /// <summary>
        /// Creates the specified file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public Entities.File Create(Entities.File file, byte[] data)
        {
            var id           = Guid.NewGuid();
            var subFolder    = (file.FileType.ToLower() == "image" ? "img/" : "doc/") + (file.AccountId.HasValue ? file.AccountId.ToString() : "common");
            var fullFolder   = Path.Combine(_path, subFolder);
            var extName      = Path.GetExtension(file.FileName)?.ToLower();
            var filePath     = Path.Combine(subFolder, id + extName);
            var fullFilePath = Path.Combine(fullFolder, id + extName);

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

            using (var stream = File.Create(fullFilePath))
            {
                stream.Write(data, 0, data.Length);
                stream.Flush();
                stream.Close();
            }

            // Data access component declarations.
            var fileDAC = new FileDAC();

            // Step 1 - Calling Create on FileDAC.
            file.Id       = id;
            file.FilePath = filePath;
            file.MimeType = FileContentType.GetMimeType(extName);

            fileDAC.Create(file);
            return(file);
        }
Example #2
0
        /// <summary>
        /// Copy a file into the same folder
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public File CopyInFolder(Guid id)
        {
            File fileNew = null;
            var  fileDAC = new FileDAC();
            var  file    = fileDAC.SelectById(id);

            if (file != null)
            {
                var toFileId    = Guid.NewGuid();
                var fullPath    = Path.Combine(_path, file.FilePath);
                var dirPath     = Path.GetDirectoryName(file.FilePath);
                var extName     = Path.GetExtension(file.FileName)?.ToLower();
                var newFileName = toFileId + extName;
                var newFullPath = Path.Combine(_path, dirPath ?? throw new InvalidOperationException(), newFileName);

                System.IO.File.Copy(fullPath, newFullPath);

                file.Id        = toFileId;
                file.FilePath  = Path.Combine(dirPath, newFileName);
                file.Timestamp = DateTime.Now;

                fileDAC.Create(file);
                fileNew    = file;
                fileNew.Id = toFileId;
            }
            return(fileNew);
        }
Example #3
0
        public Guid CreateWithCompress(Guid fileId)
        {
            var file = new FileDAC().SelectById(fileId);

            var subFolder    = CreateSubFolder(file.FileType);
            var fullFolder   = Path.Combine(_path, subFolder);
            var extName      = Path.GetExtension(file.FileName)?.ToLower();
            var filePath     = Path.Combine(subFolder, file.Id + extName);
            var fullFilePath = Path.Combine(fullFolder, file.Id + extName);

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

            using (var stream = System.IO.File.Open(fullFilePath, FileMode.Open))
            {
                var bytes = new byte[stream.Length];

                stream.Read(bytes, 0, bytes.Length);

                // 设置当前流的位置为流的开始

                stream.Seek(0, SeekOrigin.Begin);
                var compressFile = Compress(bytes, file);

                new FileDAC().Create(compressFile);
                return(compressFile.Id);
            }
        }
Example #4
0
        /// <summary>
        /// GetById business method.
        /// </summary>
        /// <param name="id">A id value.</param>
        /// <returns>Returns a File object.</returns>
        public Entities.File GetById(Guid id)
        {
            // Data access component declarations.
            var fileDAC = new FileDAC();

            // Step 1 - Calling SelectById on FileDAC.
            var result = fileDAC.SelectById(id);

            return(result);
        }
Example #5
0
        /// <summary>
        /// Checks the file exists.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public bool CheckFileExists(Guid id)
        {
            // Data access component declarations.
            var fileDAC = new FileDAC();

            var file = fileDAC.SelectById(id);

            if (file != null)
            {
                var fullPath = Path.Combine(_path, file.FilePath);
                return(System.IO.File.Exists(fullPath));
            }
            return(false);
        }
Example #6
0
        /// <summary>
        /// Deletes the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        public void Delete(Guid id)
        {
            // Data access component declarations.
            var fileDAC = new FileDAC();

            var file = fileDAC.SelectById(id);

            var fullPath = Path.Combine(_path, file.FilePath);

            System.IO.File.Delete(fullPath);

            // Step 1 - Calling Delete on FileDAC.
            fileDAC.Delete(id);
        }
Example #7
0
        /// <summary>
        /// Replace file by fileId
        /// </summary>
        /// <param name="sourceId"></param>
        /// <param name="targetId"></param>
        public void ReplaceById(Guid sourceId, Guid targetId)
        {
            var fileDAC         = new FileDAC();
            var sourceFile      = fileDAC.SelectById(sourceId);
            var targetFile      = fileDAC.SelectById(targetId);
            var stargetFileName = Path.GetFileName(targetFile.FilePath)?.ToLower();
            var sourcePath      = Path.Combine(_path, sourceFile.FilePath);
            var targetPath      = Path.Combine(_path, targetFile.FilePath);
            var targetDir       = Path.GetDirectoryName(targetPath);
            var copyPath        = Path.Combine(targetDir ?? throw new InvalidOperationException(), stargetFileName ?? throw new InvalidOperationException());

            if (System.IO.File.Exists(targetPath))
            {
                System.IO.File.Delete(targetPath);
            }
            System.IO.File.Copy(sourcePath, copyPath);
        }
Example #8
0
        public Guid[] CreateWithThumbnail(File file, byte[] data)
        {
            var originId           = Guid.NewGuid();
            var subFolder          = CreateSubFolder(file.FileType);
            var fullFolder         = Path.Combine(_path, subFolder);
            var extName            = Path.GetExtension(file.FileName)?.ToLower();
            var originFilePath     = Path.Combine(subFolder, originId + extName);
            var originfullFilePath = Path.Combine(fullFolder, originId + extName);


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

            using (var stream = System.IO.File.Create(originfullFilePath))
            {
                stream.Write(data, 0, data.Length);
                stream.Flush();
                stream.Close();
            }

            var compressFile = Compress(data, file);

            var fileDAC = new FileDAC();

            file.Id       = originId;
            file.FilePath = originFilePath;
            file.MimeType = FileContentType.GetMimeType(extName);
            file.Md5      = GetMD5HashFromByte(data);

            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 1, 30)))
            {
                fileDAC.Create(file);
                fileDAC.Create(compressFile);

                scope.Complete();
            }

            return(new Guid[] { file.Id, compressFile.Id });
        }
Example #9
0
        /// <summary>
        /// Creates the specified file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public File Create(File file, byte[] data)
        {
            var fileMd5 = GetMD5HashFromByte(data);

            var fileDAC = new FileDAC();
            var oldFile = fileDAC.SelectByMd5(fileMd5);

            var id           = Guid.NewGuid();
            var subFolder    = CreateSubFolder(file.FileType);
            var fullFolder   = Path.Combine(_path, subFolder);
            var extName      = Path.GetExtension(file.FileName)?.ToLower();
            var fullFilePath = Path.Combine(fullFolder, id + extName);

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

            if (oldFile == null)
            {
                file.FilePath = Path.Combine(subFolder, id + extName);
                using (var stream = System.IO.File.Create(fullFilePath))
                {
                    stream.Write(data, 0, data.Length);
                    stream.Flush();
                    stream.Close();
                }
            }
            else
            {
                file.FilePath = oldFile.FilePath;
            }

            file.Id       = id;
            file.MimeType = FileContentType.GetMimeType(extName);
            file.Md5      = fileMd5;
            fileDAC.Create(file);

            return(file);
        }
Example #10
0
        public Entities.File GetByMd5(string md5)
        {
            var fileDAC = new FileDAC();

            return(fileDAC.SelectByMd5(md5));
        }