Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="fileCategory"></param>
        /// <returns></returns>
        public static bool IsExist(string fileName, FileCategory fileCategory, MediaType mediaType = MediaType.Image)
        {
            bool isExist = false;

            switch (fileCategory)
            {
            case FileCategory.Profile:
                isExist = File.Exists(Path.Combine(Profile.GetDirectoryPath(fileName), MediaUtility.AddFileExtension(fileName, mediaType)));
                break;

            case FileCategory.Shared:
                Network.Server server = new Network.Server()
                {
                    LocalIP = "127.0.0.1",
                    Name    = "Current Server"
                };
                isExist = File.Exists(Path.Combine(Shared.GetDirectoryPath(server.GetServerNetworkPath(), fileName, mediaType), MediaUtility.AddFileExtension(fileName, mediaType)));
                break;

            case FileCategory.Group:

                isExist = File.Exists(Path.Combine(Group.GetDirectoryPath(fileName), MediaUtility.AddFileExtension(fileName, mediaType)));
                break;
            }
            return(isExist);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="fileCategory"></param>
        /// <returns></returns>
        public static bool Delete(string fileName, FileCategory fileCategory)
        {
            string path      = "";
            bool   isDeleted = false;

            switch (fileCategory)
            {
            case FileCategory.Profile:
                path = Path.Combine(Profile.GetDirectoryPath(fileName),
                                    MediaUtility.AddFileExtension(fileName, MediaType.Image));
                if (File.Exists(path))
                {
                    isDeleted = File.Delete(path);
                }
                else
                {
                    isDeleted = true;
                }

                break;

            case FileCategory.Shared:
                throw new NotImplementedException();
                break;

            case FileCategory.Group:

                path = Path.Combine(Group.GetDirectoryPath(fileName),
                                    MediaUtility.AddFileExtension(fileName, MediaType.Image));
                if (File.Exists(path))
                {
                    isDeleted = File.Delete(path);
                }
                else
                {
                    isDeleted = true;
                }
                break;
            }
            return(isDeleted);
        }
Beispiel #3
0
        /// <summary>
        /// Save file to the mentioned directory.
        /// </summary>
        /// <param name="file">An object containing file information and its data.</param>
        /// <returns>true if file is successfully saved; otherwise, false.</returns>
        public static void Save(File file)
        {
            string filePath = null;

            FileStream fileStream = null;

            if (file.Info.FullName != null)
            {
                filePath = Path.Combine(file.Info.FullPath, file.Info.FullName);
            }
            else
            {
                filePath = Path.Combine(file.Info.FullPath, MediaUtility.AddFileExtension(file.Info.Name, file.Info.MediaType));
            }

            if (file.Data != null)
            {
                if (System.IO.File.Exists(filePath))
                {
                    LogManager.CurrentInstance.InfoLogger.LogInfo(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Deleted avatar while updating it.");
                    System.IO.File.Delete(filePath);
                }
                try
                {
                    byte[] fileDataBinary = null;
                    fileDataBinary = Convert.FromBase64String(file.Data);
                    //fileStream = System.IO.File.OpenWrite(filePath);
                    //fileStream.Write(fileDataBinary, 0, fileDataBinary.Length);
                    using (MemoryStream stream = new MemoryStream(fileDataBinary, 0, fileDataBinary.Length))
                    {
                        System.Drawing.Image.FromStream(stream, true).Save(filePath, ImageFormat.Jpeg);
                        var fileInfo = new FileInfo(filePath);
                        file.Info.CreationTimeUtc = fileInfo.CreationTimeUtc;
                        file.Info.Length          = fileInfo.Length;
                    }
                }
                catch (FormatException formatExp)
                {
                    LogManager.CurrentInstance.ErrorLogger.LogError(
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, formatExp.Message, formatExp);
                    throw new ApplicationException(CustomHttpStatusCode.InvalidFileData.ToString("D"));
                }
                catch (System.Runtime.InteropServices.ExternalException extExp)
                {
                    LogManager.CurrentInstance.ErrorLogger.LogError(
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, extExp.Message, extExp);
                    throw new ApplicationException(CustomHttpStatusCode.FileFormatMismatched.ToString("D"));
                }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                }
            }
            else if (file.FileStream != null)
            {
                if (System.IO.File.Exists(filePath))
                {
                    LogManager.CurrentInstance.InfoLogger.LogInfo(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Deleted avatar while updating it.");
                    System.IO.File.Delete(filePath);
                }
                try
                {
                    fileStream = System.IO.File.OpenWrite(filePath);
                    if (file.FileStream.Postion > 0)
                    {
                        if (file.FileStream.Postion > fileStream.Length)
                        {
                            fileStream.Position = file.FileStream.Postion - 1;
                        }
                        else
                        {
                            throw new IndexOutOfRangeException("File seek position is invalid.");
                        }
                    }
                    file.FileStream.Stream.CopyTo(fileStream);
                    var info = new FileInfo(filePath);
                    file.Info.CreationTimeUtc = info.CreationTimeUtc;
                }
                catch (Exception exception)
                {
                    LogManager.CurrentInstance.ErrorLogger.LogError(
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, exception.Message, exception);
                    throw;
                }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                }
            }
        }