Example #1
0
        public File ReplaceFileVersion(File file, Stream fileStream)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            if (file.ID == null)
            {
                throw new ArgumentException("No file id or folder id toFolderId determine provider");
            }

            if (SetupInfo.MaxChunkedUploadSize < file.ContentLength)
            {
                throw FileSizeComment.GetFileSizeException(SetupInfo.MaxChunkedUploadSize);
            }

            if (CoreContext.Configuration.Personal && SetupInfo.IsVisibleSettings("PersonalMaxSpace"))
            {
                if (CoreContext.Configuration.PersonalMaxSpace - Global.GetUserUsedSpace(file.ID == null ? SecurityContext.CurrentAccount.ID : file.CreateBy) < file.ContentLength)
                {
                    throw FileSizeComment.GetPersonalFreeSpaceException(CoreContext.Configuration.PersonalMaxSpace);
                }
            }

            List <object> parentFoldersIds;

            lock (syncRoot)
            {
                using (var tx = dbManager.BeginTransaction())
                {
                    file.Title = Global.ReplaceInvalidCharsAndTruncate(file.Title);
                    //make lowerCase
                    file.Title = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetFileExtension(file.Title));

                    file.ModifiedBy = SecurityContext.CurrentAccount.ID;
                    file.ModifiedOn = TenantUtil.DateTimeNow();
                    if (file.CreateBy == default(Guid))
                    {
                        file.CreateBy = SecurityContext.CurrentAccount.ID;
                    }
                    if (file.CreateOn == default(DateTime))
                    {
                        file.CreateOn = TenantUtil.DateTimeNow();
                    }

                    var sql = Update("files_file")
                              .Set("version", file.Version)
                              .Set("version_group", file.VersionGroup)
                              .Set("folder_id", file.FolderID)
                              .Set("title", file.Title)
                              .Set("content_length", file.ContentLength)
                              .Set("category", (int)file.FilterType)
                              .Set("create_by", file.CreateBy.ToString())
                              .Set("create_on", TenantUtil.DateTimeToUtc(file.CreateOn))
                              .Set("modified_by", file.ModifiedBy.ToString())
                              .Set("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                              .Set("converted_type", file.ConvertedType)
                              .Set("comment", file.Comment)
                              .Set("encrypted", file.Encrypted)
                              .Set("forcesave", (int)file.Forcesave)
                              .Set("thumb", file.ThumbnailStatus)
                              .Where("id", file.ID)
                              .Where("version", file.Version);
                    dbManager.ExecuteNonQuery(sql);
                    tx.Commit();

                    file.PureTitle = file.Title;

                    parentFoldersIds = dbManager.ExecuteList(
                        new SqlQuery("files_folder_tree")
                        .Select("parent_id")
                        .Where(Exp.Eq("folder_id", file.FolderID))
                        .OrderBy("level", false)
                        ).ConvertAll(row => row[0]);

                    if (parentFoldersIds.Count > 0)
                    {
                        dbManager.ExecuteNonQuery(
                            Update("files_folder")
                            .Set("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                            .Set("modified_by", file.ModifiedBy.ToString())
                            .Where(Exp.In("id", parentFoldersIds)));
                    }
                }
            }

            if (fileStream != null)
            {
                try
                {
                    DeleteVersionStream(file);
                    SaveFileStream(file, fileStream);
                }
                catch
                {
                    if (!IsExistOnStorage(file))
                    {
                        DeleteVersion(file);
                    }
                    throw;
                }
            }

            FactoryIndexer <FilesWrapper> .IndexAsync(FilesWrapper.GetFilesWrapper(file, parentFoldersIds));

            return(GetFile(file.ID));
        }
Example #2
0
        public File SaveFile(File file, Stream fileStream, bool checkQuota = true)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (checkQuota && SetupInfo.MaxChunkedUploadSize < file.ContentLength)
            {
                throw FileSizeComment.GetFileSizeException(SetupInfo.MaxChunkedUploadSize);
            }

            if (CoreContext.Configuration.Personal && SetupInfo.IsVisibleSettings("PersonalMaxSpace"))
            {
                if (CoreContext.Configuration.PersonalMaxSpace - Global.GetUserUsedSpace(file.ID == null ? SecurityContext.CurrentAccount.ID : file.CreateBy) < file.ContentLength)
                {
                    throw FileSizeComment.GetPersonalFreeSpaceException(CoreContext.Configuration.PersonalMaxSpace);
                }
            }

            var           isNew = false;
            List <object> parentFoldersIds;

            lock (syncRoot)
            {
                using (var tx = dbManager.BeginTransaction())
                {
                    if (file.ID == null)
                    {
                        file.ID           = dbManager.ExecuteScalar <int>(new SqlQuery("files_file").SelectMax("id")) + 1;
                        file.Version      = 1;
                        file.VersionGroup = 1;
                        isNew             = true;
                    }

                    file.Title = Global.ReplaceInvalidCharsAndTruncate(file.Title);
                    //make lowerCase
                    file.Title = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetFileExtension(file.Title));

                    file.ModifiedBy = SecurityContext.CurrentAccount.ID;
                    file.ModifiedOn = TenantUtil.DateTimeNow();
                    if (file.CreateBy == default(Guid))
                    {
                        file.CreateBy = SecurityContext.CurrentAccount.ID;
                    }
                    if (file.CreateOn == default(DateTime))
                    {
                        file.CreateOn = TenantUtil.DateTimeNow();
                    }

                    dbManager.ExecuteNonQuery(
                        Update("files_file")
                        .Set("current_version", false)
                        .Where("id", file.ID)
                        .Where("current_version", true));

                    var sql = Insert("files_file")
                              .InColumnValue("id", file.ID)
                              .InColumnValue("version", file.Version)
                              .InColumnValue("version_group", file.VersionGroup)
                              .InColumnValue("current_version", true)
                              .InColumnValue("folder_id", file.FolderID)
                              .InColumnValue("title", file.Title)
                              .InColumnValue("content_length", file.ContentLength)
                              .InColumnValue("category", (int)file.FilterType)
                              .InColumnValue("create_by", file.CreateBy.ToString())
                              .InColumnValue("create_on", TenantUtil.DateTimeToUtc(file.CreateOn))
                              .InColumnValue("modified_by", file.ModifiedBy.ToString())
                              .InColumnValue("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                              .InColumnValue("converted_type", file.ConvertedType)
                              .InColumnValue("comment", file.Comment)
                              .InColumnValue("encrypted", file.Encrypted)
                              .InColumnValue("forcesave", (int)file.Forcesave)
                              .InColumnValue("thumb", file.ThumbnailStatus);
                    dbManager.ExecuteNonQuery(sql);
                    tx.Commit();

                    file.PureTitle = file.Title;

                    parentFoldersIds = dbManager.ExecuteList(
                        new SqlQuery("files_folder_tree")
                        .Select("parent_id")
                        .Where(Exp.Eq("folder_id", file.FolderID))
                        .OrderBy("level", false)
                        ).ConvertAll(row => row[0]);

                    if (parentFoldersIds.Count > 0)
                    {
                        dbManager.ExecuteNonQuery(
                            Update("files_folder")
                            .Set("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                            .Set("modified_by", file.ModifiedBy.ToString())
                            .Where(Exp.In("id", parentFoldersIds)));
                    }

                    if (isNew)
                    {
                        RecalculateFilesCount(dbManager, file.FolderID);
                    }
                }
            }

            if (fileStream != null)
            {
                try
                {
                    SaveFileStream(file, fileStream);
                }
                catch (Exception saveException)
                {
                    try
                    {
                        if (isNew)
                        {
                            var stored = Global.GetStore().IsDirectory(GetUniqFileDirectory(file.ID));
                            DeleteFile(file.ID, stored);
                        }
                        else if (!IsExistOnStorage(file))
                        {
                            DeleteVersion(file);
                        }
                    }
                    catch (Exception deleteException)
                    {
                        throw new Exception(saveException.Message, deleteException);
                    }
                    throw;
                }
            }

            FactoryIndexer <FilesWrapper> .IndexAsync(FilesWrapper.GetFilesWrapper(file, parentFoldersIds));

            return(GetFile(file.ID));
        }