Exemple #1
0
        internal static string FileEntityToPath(IUnitOfWork uow, tbl_User user, tbl_UserFile file)
        {
            var path  = string.Empty;
            var paths = new List <string> {
            };

            var folder = uow.UserFolders.Get(QueryExpressionFactory.GetQueryExpression <tbl_UserFolder>()
                                             .Where(x => x.IdentityId == user.IdentityId && x.Id == file.FolderId).ToLambda())
                         .Single();

            while (folder.ParentId != null)
            {
                paths.Add(folder.VirtualName);
                folder = folder.Parent;
            }

            for (int i = paths.Count() - 1; i >= 0; i--)
            {
                path += "/" + paths.ElementAt(i);
            }

            path += "/" + file.VirtualName;

            return(path);
        }
        protected override FileNode CreateFile(DirectoryNode parent, FileNode child)
        {
            FileInfo file = null;

            try
            {
                using (var scope = _factory.CreateScope())
                {
                    var uow  = scope.ServiceProvider.GetRequiredService <IUnitOfWork>();
                    var conf = scope.ServiceProvider.GetRequiredService <IConfiguration>();

                    var folderEntity = CompositeFileSystemHelper.FolderPathToEntity(uow, _userEntity, parent.Path.StringPath);
                    var filePath     = Strings.GetDirectoryHash($"{_userEntity.ToString()}{parent.Path.StringPath}{child.Name}");
                    var fileName     = Hashing.MD5.Create(Guid.NewGuid().ToString());
                    var now          = DateTime.UtcNow;

                    var folder = new DirectoryInfo(conf["Storage:UnstructuredDataPath"]
                                                   + Path.DirectorySeparatorChar + filePath);

                    if (!folder.Exists)
                    {
                        folder.Create();
                    }

                    file = new FileInfo(conf["Storage:UnstructuredDataPath"]
                                        + Path.DirectorySeparatorChar + filePath
                                        + Path.DirectorySeparatorChar + fileName);

                    var fileEntity = new tbl_UserFile
                    {
                        Id           = Guid.NewGuid(),
                        IdentityId   = _userEntity.IdentityId,
                        FolderId     = folderEntity.Id,
                        VirtualName  = child.Name,
                        RealPath     = filePath,
                        RealFileName = fileName,
                        ReadOnly     = false,
                        Created      = now,
                        LastAccessed = null,
                        LastUpdated  = null,
                        LastVerified = now,
                    };

                    using (var sha256 = new SHA256Managed())
                        using (var fs = new FileStream(file.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            var hash = sha256.ComputeHash(fs);

                            fileEntity.RealFileSize = fs.Length;
                            fileEntity.HashSHA256   = Strings.GetHexString(hash);
                        }

                    uow.UserFiles.Create(fileEntity);
                    uow.Commit();
                }

                return(child);
            }
            catch (Exception ex) when(ex is DbUpdateException || ex is DbUpdateConcurrencyException)
            {
                if (file.Exists)
                {
                    file.Delete();
                }

                Log.Error(ex.ToString());
                throw;
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
                throw;
            }
        }
Exemple #3
0
        internal static tbl_UserFile SaveFileStream(IConfiguration conf, Stream content, tbl_UserFile fileEntity)
        {
            var folder = new DirectoryInfo(conf["Storage:UnstructuredDataPath"]
                                           + Path.DirectorySeparatorChar + fileEntity.RealPath);

            if (!folder.Exists)
            {
                folder.Create();
            }

            var file = new FileInfo(conf["Storage:UnstructuredDataPath"]
                                    + Path.DirectorySeparatorChar + fileEntity.RealPath
                                    + Path.DirectorySeparatorChar + fileEntity.RealFileName);

            using (var fs = new FileStream(file.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                content.CopyTo(fs);

            using (var sha256 = new SHA256Managed())
                using (var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var hash = sha256.ComputeHash(fs);

                    fileEntity.RealFileSize = fs.Length;
                    fileEntity.HashSHA256   = Strings.GetHexString(hash);
                    fileEntity.ReadOnly     = false;
                    fileEntity.Created      = DateTime.UtcNow;
                    fileEntity.LastAccessed = null;
                    fileEntity.LastUpdated  = null;
                }

            return(fileEntity);
        }