public bool DeleteThumbnailFor(Model.FileModel deleteThumbnailForFile)
        {
            if (deleteThumbnailForFile == null)
            {
                return(false);
            }
            // get file extension and compose thumbnail path
            FileInfo fi            = new FileInfo(deleteThumbnailForFile.Name);
            string   thumbnailPath = _config.LocalFSThumbsDirectoryPath + Path.DirectorySeparatorChar
                                     + deleteThumbnailForFile.Hash + fi.Extension;

            if (!File.Exists(thumbnailPath))
            {
                return(false);
            }

            try
            {
                File.Delete(thumbnailPath);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public Model.FileModel RenameFile(Model.FileModel fileToChange, string newname)
        {
            if (fileToChange == null)
            {
                return(null);
            }

            string   path = DecodeHashToPath(fileToChange.Hash);
            FileInfo fi   = new FileInfo(path);

            string parentDir = fi.Directory.FullName;

            if (parentDir.Last() != Path.DirectorySeparatorChar)
            {
                parentDir += Path.DirectorySeparatorChar;
            }
            string newPath = parentDir + newname;

            if (File.Exists(newPath))
            {
                return(null);
            }
            try
            {
                File.Move(path, newPath);
                FileInfo newFileInfo = new FileInfo(newPath);
                return(createFileModel(newFileInfo, EncodePathToHash(parentDir)));
            }
            catch
            {
                return(null);
            }
        }
예제 #3
0
        /// <summary>
        /// 文件对比
        /// </summary>
        /// <param name="file"></param>
        /// <param name="forders"></param>
        /// <returns>0相等,1新增,2修改,3删除</returns>
        private static int FileCompare(Model.FileModel file, List <string> forders)
        {
            if (_rootForder == null)
            {
                InitCurrentFiles();
            }
            bool isDelete;
            bool isExist;

            Model.ForderModel fo = GetForderFiles(forders);
            foreach (var item in fo.Files)
            {
                if (item.File_Name == file.File_Name)
                {
                    isExist = true;
                    if (item.File_Modify_Time == file.File_Modify_Time)
                    {
                        //名称和文件修改时间一样,相等
                        return(0);
                    }
                    else
                    {
                        return(2);
                    }
                }
            }

            //默认不存在,新增
            return(1);
        }
        public Response.IResponse Execute(CommandArgs args)
        {
            var pa = args.As <putArgs>();

            if (string.IsNullOrWhiteSpace(pa.target))
            {
                return(new Response.ErrorResponse("target not specified"));
            }

            // get volume for our target
            var vol = _volumeManager.GetByHash(pa.target);

            if (vol == null)
            {
                return(new Response.ErrorResponse("invalid target"));
            }

            var fileToModify = vol.GetFileByHash(pa.target);

            if (fileToModify == null)
            {
                return(new Response.ErrorResponse("invalid target"));
            }

            Model.FileModel modifiedFile = vol.SetTextFileContent(fileToModify, pa.content);
            if (modifiedFile == null)
            {
                return(new Response.ErrorResponse("error setting file content"));
            }
            return(new Response.PutResponse(new Model.FileModel[] { modifiedFile }));
        }
예제 #5
0
        public async Task <ActionResult> Post()
        {
            var    data         = Request.Form.Files["data"];
            string lastModified = Request.Form["lastModified"].ToString();
            var    total        = Request.Form["total"];
            var    fileName     = Request.Form["fileName"];
            var    index        = Request.Form["index"];
            var    fileSize     = long.Parse(Request.Form["fileSize"]);

            string temporaryFile = Path.Combine($"{Directory.GetCurrentDirectory()}/wwwroot/", lastModified);

            try
            {
                if (!Directory.Exists(temporaryFile))
                {
                    Directory.CreateDirectory(temporaryFile);
                }
                string tempPath = Path.Combine(temporaryFile, index.ToString());
                if (!Convert.IsDBNull(data))
                {
                    await FileHelper.CreateFileAsync(data, tempPath);
                }
                bool mergeOk = false;
                if (total == index)
                {
                    var fileExtension = Path.GetExtension(fileName).ToLowerInvariant();
                    fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
                    var finalPath = Path.Combine($"{Directory.GetCurrentDirectory()}/wwwroot/", fileName);//最终的文件名(demo中保存的是它上传时候的文件名,实际操作肯定不能这样)
                    mergeOk = await FileHelper.MergeFileAsync(lastModified, finalPath);

                    if (mergeOk)
                    {
                        var saveFile = new Model.FileModel
                        {
                            FileName      = fileName,
                            FilePath      = finalPath,
                            ExtensionName = fileExtension,
                            FileSize      = fileSize
                        };
                        await _fileRepository.AddAsync(saveFile);
                    }
                }

                Dictionary <string, object> result = new Dictionary <string, object>();
                result.Add("number", index);
                result.Add("mergeOk", mergeOk);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                Directory.Delete(temporaryFile);
                throw ex;
            }
        }
        public Model.FileModel CopyFile(Model.FileModel fileToCopy, string destinationDirectory,
                                        bool cut)
        {
            if (fileToCopy == null || string.IsNullOrWhiteSpace(destinationDirectory))
            {
                return(null);
            }
            if (!Directory.Exists(destinationDirectory))
            {
                return(null);
            }

            string   path = DecodeHashToPath(fileToCopy.Hash);
            FileInfo fi   = new FileInfo(path);
            // compose final directory
            string destDir = destinationDirectory;

            if (destDir.Last() != Path.DirectorySeparatorChar)
            {
                destDir += Path.DirectorySeparatorChar;
            }
            string newPath = destDir + fi.Name;

            if (File.Exists(newPath))
            {
                return(null);
            }

            try
            {
                // check for cut or copy
                if (cut)
                {
                    File.Move(path, newPath);
                }
                else
                {
                    File.Copy(path, newPath);
                }

                FileInfo newFileInfo = new FileInfo(newPath);
                return(createFileModel(newFileInfo, EncodePathToHash(destDir)));
            }
            catch
            {
                return(null);
            }
        }
        public string GetTextFileContent(Model.FileModel fileToGet)
        {
            if (fileToGet == null)
            {
                return(null);
            }

            string path = DecodeHashToPath(fileToGet.Hash);

            try
            {
                return(File.ReadAllText(path));
            }
            catch
            {
                return(null);
            }
        }
예제 #8
0
        //根据文件路径得到文件所引用的js css文件列表
        public static async Task <Model.FileModel> ReadFile(string filePath)
        {
            var file   = new Model.FileModel(filePath);
            var stream = new StreamReader(filePath);
            var line   = await stream.ReadLineAsync();

            do
            {
                if (Regex.IsMatch(line, "< *?link.*?href=.*?>"))
                {
                    file.Links.Add(line);
                }
                else if (Regex.IsMatch(line, "< *?script.*?src.*?>"))
                {
                    file.Scripts.Add(line);
                }
                line = await stream.ReadLineAsync();
            } while (line != null);
            return(file);
        }
        public Model.FileModel DuplicateFile(Model.FileModel fileToDuplicate)
        {
            if (fileToDuplicate == null)
            {
                return(null);
            }

            string path = DecodeHashToPath(fileToDuplicate.Hash);

            FileInfo fi = new FileInfo(path);
            // compose final file path
            string destDir = fi.DirectoryName;

            if (destDir.Last() != Path.DirectorySeparatorChar)
            {
                destDir += Path.DirectorySeparatorChar;
            }
            string newPath = destDir + string.Format(_config.DuplicateFilePattern, fi.Name, fi.Extension);

            // new file should be here
            if (File.Exists(newPath))
            {
                return(null);
            }

            try
            {
                File.Copy(path, newPath);

                FileInfo newFileInfo = new FileInfo(newPath);
                return(createFileModel(newFileInfo, EncodePathToHash(destDir)));
            }
            catch
            {
                return(null);
            }
        }
        public bool DeleteFile(Model.FileModel fileToDelete)
        {
            if (fileToDelete == null)
            {
                return(false);
            }

            string path = DecodeHashToPath(fileToDelete.Hash);

            if (!File.Exists(path))
            {
                return(false);
            }

            try
            {
                File.Delete(path);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
        public Model.FileModel SetTextFileContent(Model.FileModel fileToModify, string content)
        {
            if (fileToModify == null)
            {
                return(null);
            }

            string path = DecodeHashToPath(fileToModify.Hash);

            if (!File.Exists(path))
            {
                return(null);
            }

            try
            {
                File.WriteAllText(path, content);
                return(createFileModel(new FileInfo(path), fileToModify.ParentHash));
            }
            catch
            {
                return(null);
            }
        }
예제 #12
0
        ///// <summary>
        ///// 获取文件最新的版本
        ///// </summary>
        ///// <param name="fileName"></param>
        ///// <param name="forderId"></param>
        ///// <returns></returns>
        //public Model.FileModel GetFileLastVer(string fileName, List<string> forders)
        //{
        //    return dal.GetFileLastVer(fileName, forders);
        //}

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.FileModel model)
        {
            return(dal.Add(model));
        }
예제 #13
0
 public FileViewModel(Model.FileModel model) : base(model)
 {
 }
예제 #14
0
 /// <summary>
 /// 更新一条数据
 /// </summary>
 public bool Update(Model.FileModel model)
 {
     return(dal.Update(model));
 }
 public MkfileResponse(Model.FileModel createdFile)
 {
     Added = new Model.FileModel[] { createdFile };
 }
 public RenameResponse(string oldFileHash, Model.FileModel renamedFile)
 {
     Added   = new Model.FileModel[] { renamedFile };
     Removed = new string[] { oldFileHash };
 }
예제 #17
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.FileModel model)
        {
            //Update_UserId,Update_UserName,File_Name,File_LastVersion,File_Md5,File_DirId,
            //File_Type,File_Size,File_Ext,File_SmallImage,File_LargeImage,Content,Add_Time,Remark,File_Modify_Time
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + databaseprefix + "File set ");
            strSql.Append("Update_UserId=@Update_UserId,");
            strSql.Append("Update_UserName=@Update_UserName,");
            strSql.Append("File_Name=@File_Name,");
            strSql.Append("File_LastVersion=@File_LastVersion,");
            strSql.Append("File_Md5=@File_Md5,");
            strSql.Append("File_DirId=@File_DirId,");
            strSql.Append("File_Type=@File_Type,");
            strSql.Append("File_Size=@File_Size,");
            strSql.Append("File_Ext=@File_Ext,");
            strSql.Append("Add_Time=@Add_Time,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("File_Modify_Time=@File_Modify_Time");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@id",               SqlDbType.Int,          4),
                new SqlParameter("@Update_UserId",    SqlDbType.Int,          4),
                new SqlParameter("@Update_UserName",  SqlDbType.NVarChar,   100),
                new SqlParameter("@File_Name",        SqlDbType.NVarChar,   100),
                new SqlParameter("@File_LastVersion", SqlDbType.Int),
                new SqlParameter("@File_Md5",         SqlDbType.NVarChar,   100),
                new SqlParameter("@File_DirId",       SqlDbType.Int),
                new SqlParameter("@File_Type",        SqlDbType.NVarChar,   100),
                new SqlParameter("@File_Size",        SqlDbType.Int),
                new SqlParameter("@File_Ext",         SqlDbType.NVarChar,   100),
                new SqlParameter("@Add_Time",         SqlDbType.DateTime),
                new SqlParameter("@Remark",           SqlDbType.NVarChar,  1000),
                new SqlParameter("@File_Modify_Time", SqlDbType.BigInt),
            };
            parameters[0].Value  = model.ID;
            parameters[1].Value  = model.UserId;
            parameters[2].Value  = model.UserName;
            parameters[3].Value  = model.File_Name;
            parameters[4].Value  = model.File_LastVersion;
            parameters[5].Value  = model.File_Md5;
            parameters[6].Value  = model.File_DirId;
            parameters[7].Value  = model.File_Type;
            parameters[8].Value  = model.File_Size;
            parameters[9].Value  = model.File_Ext;
            parameters[10].Value = model.Add_Time;
            parameters[11].Value = model.Remark;
            parameters[12].Value = model.File_Modify_Time;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
		public RenameResponse( string oldFileHash, Model.FileModel renamedFile )
		{
			Added = new Model.FileModel[] { renamedFile };
			Removed = new string[] { oldFileHash };
		}