Пример #1
0
        /// <summary>
        /// return GUID as string (valid)
        /// </summary>
        /// <param name="guid">a GUID to be formated</param>
        /// <param name="format">valid format</param>
        /// <returns>formatted GUID string</returns>
        /// <see href="https://msdn.microsoft.com/en-us/library/97af8hh4(v=vs.110).aspx">Guid.ToString Method (String)</see>
        private static string FormatGuidString(Guid guid, ValidFormat format)
        {
            switch (format)
            {
            case ValidFormat.TypeN:
                return(guid.ToString("N"));

            case ValidFormat.TypeD:
                return(guid.ToString("D"));

            case ValidFormat.TypeB:
                return(guid.ToString("B"));

            case ValidFormat.TypeP:
                return(guid.ToString("P"));

            case ValidFormat.TypeX:
                return(guid.ToString("X"));

            case ValidFormat.TypeOLECREATE:
                return(FormatGuidAsImplementOleCreate(guid));

            case ValidFormat.TypeDEFINE_GUID:
                return(FormatGuidAsDefineGuid(guid));

            default:
                throw new ArgumentException(format.ToString());
            }
        }
Пример #2
0
        /// <returns>true if success, otherwise false</returns>
        public static bool SignUp(string username, string password, string email)
        {
            if (password.Length < User.MinimumPasswordLength || !ValidFormat.IsEmailFormat(email))
            {
                return(false);
            }

            int returnID = DataAccessLayer.InsertCommand_SpecificColumnAutoID(User.LoginTable,
                                                                              "Username, Password, Email", ":username, :password, :email", User.LoginTableID,
                                                                              new CommandParameter(":username", username),
                                                                              new CommandParameter(":password", Encryption.Encrypt(password, username)),
                                                                              new CommandParameter(":email", email));

            return(returnID == 0);
        }
Пример #3
0
        public void IsEmailFormatTest()
        {
            // sample valid email format for test
            string[] validEmailFromatList =
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**"
            };

            // sample invalid email format for test
            string[] invalidEmailFormatList =
            {
                "Abc.example.com",
                "A@b@[email protected]",
                "a\"b(c)d,e:f;g<h>i[j\\k][email protected]",
                "just\"not\"*****@*****.**",
                "this is\"not\\[email protected]",
                "this\\ still\\\"not\\\\[email protected]",
                "*****@*****.**",
                "*****@*****.**",
                " [email protected]",
                "[email protected] "
            };

            // check valid email format
            foreach (string email in validEmailFromatList)
            {
                Assert.IsTrue(ValidFormat.IsEmailFormat(email),
                              "Valid email format not detected for: \"" + email + "\".");
            }

            // check invalid email format
            foreach (string email in invalidEmailFormatList)
            {
                Assert.IsFalse(ValidFormat.IsEmailFormat(email),
                               "Invalid email format not detected for: \"" + email + "\".");
            }
        }
        public const int DefaultMaxSize = 500 * 1024;    // 512 KB



        //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
        //  @param enum ValidFormat : so you Can Easily Pass Valid Format For File and get back the right Conditions and Constraints to validate that file
        //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
        public static FileValidationConstraints fileValidationConstraints(ValidFormat fileFormat)
        {
            FileValidationConstraints Constraints;

            switch (fileFormat)
            {
            case ValidFormat.Img:
                Constraints = ImgConstraints();
                break;

            case ValidFormat.document:
                Constraints = DocumentConstraints();
                break;

            case ValidFormat.MixDocImg:
                Constraints = MixDocAndImgConstraints();
                break;

            default:
                throw new Exception(" You must specify some conditions and restrictions to adjust the file validation process");
            }
            return(Constraints);
        }
Пример #5
0
 public _ValidFile(ValidFormat _fileFormat)
 {
     fileFormat = _fileFormat;
 }
Пример #6
0
        //--------------------------------------------------------------------------------------------------------------------
        // Usage Example (How To Use DownloadAsync in Controller)
        //--------------------------------------------------------------------------------------------------------------------

        /* 1- inject IHostingEnvironment _hostingEnvironment
         * 2-
         *  string uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "uploads");
         *  string FullPath = Path.Combine(uploadFolder, fileName);
         *  var f = await GenericClasses.ManagingFiles.FileManager.DownloadAsync(FullPath);
         */


        //--------------------------------------------------------------------------------------------------------------------
        // Explanation
        //--------------------------------------------------------------------------------------------------------------------

        /* this class is responsible for Upload , Download  , Delete Files from and to the Server
         *
         * 1-UploadFileWithValidationAsync : perform upload functionality with validation on file extention and mimtype and so on
         *                  Related Functions => Enum:ValidFormat , Func:GetUniqueFileName()
         *
         *                  Params:
         *                          -File
         *                          -ValidFormat => type of file to valid it if its image or document like word or xlx ..
         *                          -uploadDirectory  example=> ServerPath/wwwroot/Uploads
         *
         * 2-UploadFileAsync : perform upload functionality with out validation so you have to validate the file before using it if you need
         *
         * 3-DownloadAsync  : Note that Method not download actual file but its return object of class its properties used by FileStreamResult in controller action to return file
         *                  Params:
         *                          -uploadDirectory  example=> ServerPath/wwwroot/Uploads/fileName.jpg => So We Can Download File From This Folder by file name in this path
         *
         *                 Related Functions =>
         *
         *                          -Note That we have Created Custom Class its name (MyFileDetails) this Class has 3 simple propertise MemoryFileContent , ContentType , FileName and these 3 attributes used by FileStreamResult in controller action to return file
         *                          -GetContentType() and GetMimeTypes() => used to get GetContentType of downloaded file
         *
         * 4-RemoveFileFromServer : This method used to remove file from server
         *                  Params:
         *                          -Full Path Of file to delete it
         */


        //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


        //--------------------------------------------------------------------------------------------------------------------
        // perform upload functionality with validation on file extention and mimtype and so on
        // Return :
        //      1-Upload successful : Return FilePath
        //      2-Upload failed     : Return Null
        //--------------------------------------------------------------------------------------------------------------------
        public static async Task <string> UploadFileWithValidationAsync(IFormFile file, ValidFormat fileFormat, string webRootPath, string uploadDirectory)
        {
            bool isValidFormat = false;


            if (file == null || file.Length == 0)
            {
                return(null);
            }

            // Get Default Constraints For Uploaded file to Validate it like {Allowed mimeTypes,MaxSize,Allowed Extentions}
            var Constraints = FileSettings.fileValidationConstraints(fileFormat);

            isValidFormat = ValidateFiles.IsValidFile(file, Constraints);

            if (isValidFormat)
            {
                return(await UploadFileAsync(file, webRootPath, uploadDirectory));
            }

            // Not Valid File
            return(null);
        }