Ejemplo n.º 1
0
        public Contract_VideoLocalRenamed RenameFile(int videoLocalID, string renameRules)
        {
            Contract_VideoLocalRenamed ret = new Contract_VideoLocalRenamed();
            ret.VideoLocalID = videoLocalID;
            ret.Success = true;
            try
            {
                VideoLocalRepository repVids = new VideoLocalRepository();
                VideoLocal vid = repVids.GetByID(videoLocalID);
                if (vid == null)
                {
                    ret.VideoLocal = null;
                    ret.NewFileName = string.Format("ERROR: Could not find file record");
                    ret.Success = false;
                }
                else
                {
                    ret.VideoLocal = null;
                    ret.NewFileName = RenameFileHelper.GetNewFileName(vid, renameRules);

                    if (!string.IsNullOrEmpty(ret.NewFileName))
                    {
                        // check if the file exists
                        string fullFileName = vid.FullServerPath;
                        if (!File.Exists(fullFileName))
                        {
                            ret.NewFileName = "Error could not find the original file";
                            ret.Success = false;
                            return ret;
                        }

                        // actually rename the file
                        string path = Path.GetDirectoryName(fullFileName);
                        string newFullName = Path.Combine(path, ret.NewFileName);

                        try
                        {
                            logger.Info(string.Format("Renaming file From ({0}) to ({1})....", fullFileName, newFullName));

                            if (fullFileName.Equals(newFullName, StringComparison.InvariantCultureIgnoreCase))
                            {
                                logger.Info(string.Format("Renaming file SKIPPED, no change From ({0}) to ({1})", fullFileName, newFullName));
                                ret.NewFileName = newFullName;
                            }
                            else
                            {
                                File.Move(fullFileName, newFullName);
                                logger.Info(string.Format("Renaming file SUCCESS From ({0}) to ({1})", fullFileName, newFullName));
                                ret.NewFileName = newFullName;

                                string newPartialPath = "";
                                int folderID = vid.ImportFolderID;
                                ImportFolderRepository repFolders = new ImportFolderRepository();

                                DataAccessHelper.GetShareAndPath(newFullName, repFolders.GetAll(), ref folderID, ref newPartialPath);

                                vid.FilePath = newPartialPath;
                                repVids.Save(vid);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Info(string.Format("Renaming file FAIL From ({0}) to ({1}) - {2}", fullFileName, newFullName, ex.Message));
                            logger.ErrorException(ex.ToString(), ex);
                            ret.Success = false;
                            ret.NewFileName = ex.Message;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
                ret.VideoLocal = null;
                ret.NewFileName = string.Format("ERROR: {0}", ex.Message);
                ret.Success = false;
            }
            return ret;
        }
Ejemplo n.º 2
0
        public Contract_VideoLocalRenamed RenameFilePreview(int videoLocalID, string renameRules)
        {
            Contract_VideoLocalRenamed ret = new Contract_VideoLocalRenamed();
            ret.VideoLocalID = videoLocalID;
            ret.Success = true;

            try
            {
                VideoLocalRepository repVids = new VideoLocalRepository();
                VideoLocal vid = repVids.GetByID(videoLocalID);
                if (vid == null)
                {
                    ret.VideoLocal = null;
                    ret.NewFileName = string.Format("ERROR: Could not find file record");
                    ret.Success = false;
                }
                else
                {
                    if (videoLocalID == 726)
                        Debug.Write("test");

                    ret.VideoLocal = null;
                    ret.NewFileName = RenameFileHelper.GetNewFileName(vid, renameRules);
                    if (string.IsNullOrEmpty(ret.NewFileName)) ret.Success = false;
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
                ret.VideoLocal = null;
                ret.NewFileName = string.Format("ERROR: {0}", ex.Message);
                ret.Success = false;
            }
            return ret;
        }
        public Contract_VideoLocalRenamed RenameFile(int videoLocalID, string renameRules)
        {
            Contract_VideoLocalRenamed ret = new Contract_VideoLocalRenamed();
            ret.VideoLocalID = videoLocalID;
            ret.Success = true;
            try
            {
                VideoLocal vid = RepoFactory.VideoLocal.GetByID(videoLocalID);
                if (vid == null)
                {
                    ret.VideoLocal = null;
                    ret.NewFileName = string.Format("ERROR: Could not find file record");
                    ret.Success = false;
                }
                else
                {
                    ret.VideoLocal = null;
                    ret.NewFileName = RenameFileHelper.GetNewFileName(vid, renameRules);

                    if (!string.IsNullOrEmpty(ret.NewFileName))
                    {
                        string name = string.Empty;
                        if (vid.Places.Count > 0)
                        {
                            foreach (VideoLocal_Place place in vid.Places)
                            {
                                // check if the file exists
                                string fullFileName = place.FullServerPath;
                                IFileSystem fs = place.ImportFolder.FileSystem;
                                FileSystemResult<IObject> obj = fs.Resolve(fullFileName);
                                if (!obj.IsOk || obj.Result is IDirectory)
                                {
                                    ret.NewFileName = "Error could not find the original file";
                                    ret.Success = false;
                                    return ret;
                                }

                                // actually rename the file
                                string path = Path.GetDirectoryName(fullFileName);
                                string newFullName = Path.Combine(path, ret.NewFileName);

                                try
                                {
                                    logger.Info(string.Format("Renaming file From ({0}) to ({1})....", fullFileName,
                                        newFullName));

                                    if (fullFileName.Equals(newFullName, StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        logger.Info(string.Format(
                                            "Renaming file SKIPPED, no change From ({0}) to ({1})",
                                            fullFileName, newFullName));
                                        ret.NewFileName = newFullName;
                                    }
                                    else
                                    {
                                        string dir = Path.GetDirectoryName(newFullName);

                                        ((IFile) obj.Result).Rename(ret.NewFileName);
                                        logger.Info(string.Format("Renaming file SUCCESS From ({0}) to ({1})",
                                            fullFileName,
                                            newFullName));
                                        ret.NewFileName = newFullName;
                                        var tup = VideoLocal_PlaceRepository.GetFromFullPath(newFullName);
                                        place.FilePath = tup.Item2;
                                        name = Path.GetFileName(tup.Item2);
                                        RepoFactory.VideoLocalPlace.Save(place);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    logger.Info(string.Format("Renaming file FAIL From ({0}) to ({1}) - {2}",
                                        fullFileName,
                                        newFullName, ex.Message));
                                    logger.Error( ex,ex.ToString());
                                    ret.Success = false;
                                    ret.NewFileName = ex.Message;
                                }
                            }
                            vid.FileName = name;
                            RepoFactory.VideoLocal.Save(vid,true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error( ex,ex.ToString());
                ret.VideoLocal = null;
                ret.NewFileName = string.Format("ERROR: {0}", ex.Message);
                ret.Success = false;
            }
            return ret;
        }