Example #1
0
        /// <summary>
        /// Gets a key based on a filename
        /// Please consider security implication of selecting a key - https://www.owasp.org/index.php/Unrestricted_File_Upload
        /// </summary>
        public string GetKey(string fileName, string contentType, string delimiter)
        {
            Contract.NotNullOrEmpty(fileName, nameof(fileName));
            Contract.NotNullOrEmpty(contentType, nameof(contentType));

            string guid = Guid.NewGuid().ToString();

            var invalid = Path.GetInvalidFileNameChars().Union(Path.GetInvalidPathChars());

            foreach (char c in invalid)
            {
                fileName = fileName.Replace(c.ToString(), "_");
            }

            int maxUploadFileNameLength = MaxFileNameLength - UploadExt.Length;

            if (fileName.Length > maxUploadFileNameLength)
            {
                string ext = Path.GetExtension(fileName);
                fileName  = fileName.Substring(0, maxUploadFileNameLength - ext.Length - TrimmedSuffix.Length);
                fileName += TrimmedSuffix + ext;
            }

            fileName += UploadExt;

            string key = KeyUtils.JoinKeys(guid, fileName, delimiter);

            return(key);
        }
Example #2
0
        public void Join_Keys(string prefix, string key, string delimiter, string expected)
        {
            string actual = KeyUtils.JoinKeys(prefix, key, delimiter);

            Assert.Equal(expected, actual);
        }
Example #3
0
 private static string GetKey(string key, AmazonS3CabinetConfig config)
 {
     return(KeyUtils.JoinKeys(config.KeyPrefix, key, config.Delimiter));
 }