コード例 #1
0
        /// <summary>
        ///  Create file file and add it in branch
        /// </summary>
        /// <param Metafile="metafile">Object represent a file</param>
        public async Task CreateFile(Metafile metafile)
        {
            // Search branch
            var branchToUpdate = await this.GetBranch(metafile.BranchId);

            if (branchToUpdate == null)
            {
                _logger.Log(LogLevel.Warn, LOG_CATEGORY, $"BranchNotFound : {metafile.BranchId} ", new { Environment.StackTrace });
                throw new BranchException($"BranchNotFound: {metafile.BranchId}");
            }

            // Update head
            var headFile = branchToUpdate.MetafilesHead.FirstOrDefault <MetafileHead>(mf => mf.Id == metafile.BranchId + "_" + metafile.Path + "_HEAD");

            if (headFile != null)
            {
                _logger.Log(LogLevel.Warn, LOG_CATEGORY, $"File {metafile.Path} already exist on server ", new { });
                headFile.Record   = metafile;
                headFile.HeadFile = metafile.Id;
                headFile.IsActive = true;
            }
            else
            {
                _logger.Log(LogLevel.Warn, LOG_CATEGORY, $"File {metafile.Path} add on server ", new { });
                //Add new head in branch and index file index flatten.
                var metaFileHead = new MetafileHead();
                metaFileHead.Id       = metafile.BranchId + "_" + metafile.Path + "_HEAD";
                metaFileHead.HeadFile = metafile.Id;
                metaFileHead.IsActive = true;
                metaFileHead.Record   = metafile;
                branchToUpdate.MetafilesHead.Add(metaFileHead);
            }

            // Remove branch in cache
            Task <Branch> removedTask;

            _branches.TryRemove(metafile.BranchId, out removedTask);

            //Update branch
            await this.AddOrUpdateBranch(branchToUpdate);

            //Update file
            await this.AddMetafile(metafile);
        }
コード例 #2
0
        /// <summary>
        ///  Remove branch.
        /// </summary>
        /// <param BranchName="branchName">Name of branch</param>
        /// <param Path="path">Path in branch.</param>
        /// <returns>Return true if the file delete in database</returns>
        public async Task <bool> DeleteFile(string branchName, string path)
        {
            var branch = await this.GetBranch(branchName);

            if (branch == null)
            {
                _logger.Log(LogLevel.Warn, LOG_CATEGORY, $"BranchNotFound : {branchName} ", new { Environment.StackTrace });
                throw new BranchException($"BranchNotFound : {branchName}");
            }

            // Todo asset check si le head existe
            MetafileHead fileToDelete = branch.MetafilesHead.FirstOrDefault(mf => mf.Id == branchName + "_" + path + "_HEAD");

            if (fileToDelete == null)
            {
                _logger.Log(LogLevel.Error, LOG_CATEGORY, $"Specified file not found on Elasticsearch", new { path });
                throw new FileException($"Specified file not found on Elasticsearch. Original file : { path }");
            }

            // Remove branch in cache
            Task <Branch> removedTask;

            _branches.TryRemove(branchName, out removedTask);

            // Todo asset mettre la branche en désactiver à la place de la supprimer
            // Try to update branch without file
            foreach (var mfh in branch.MetafilesHead)
            {
                if (mfh.Id == fileToDelete.Id)
                {
                    mfh.IsActive = false;
                }
            }

            var updatedBranch = await this.AddOrUpdateBranch(branch);

            if (updatedBranch == null)
            {
                return(false);
            }

            return(true);
        }