Beispiel #1
0
        /// <summary>
        /// Returns full path to a file after saving it to Azure Blob Storage.
        /// </summary>
        /// <param name="userId">The id of the user who uploads.</param>
        /// <param name="fileName">Slugged filename with ext.</param>
        /// <param name="year">Upload year.</param>
        /// <param name="month">Upload month.</param>
        /// <param name="content">The content of file.</param>
        /// <param name="appId">Which app it uploaded it.</param>
        /// <returns></returns>
        public async Task <string> SaveFileAsync(int userId, string fileName, string year, string month, byte[] content, EAppType appId)
        {
            // blobName "blog/1/2017/11/filename", container is "media"
            string blobName = string.Format("{0}/{1}/{2}/{3}/{4}",
                                            appId.ToString().ToLowerInvariant(),
                                            userId,
                                            year,
                                            month,
                                            fileName);

            // get a ref to blob which does not call server
            var blob = _container.GetBlockBlobReference(blobName);

            // make sure blob is unique
            int i = 1;

            while (await blob.ExistsAsync())
            {
                blobName = blobName.Insert(blobName.LastIndexOf('.'), $"-{i}");
                blob     = _container.GetBlockBlobReference(blobName);
            }

            // set content type
            blob.Properties.ContentType = MimeTypeMap.GetMimeType(Path.GetExtension(fileName));

            // create blob with contents
            await blob.UploadFromByteArrayAsync(content, 0, content.Length);

            return(blob.Uri.ToString());
        }
        /// <summary>
        /// Returns relative path to a file after saving it on the file system.
        /// </summary>
        /// <param name="userId">The id of the user who uploads.</param>
        /// <param name="fileName">Slugged filename with ext.</param>
        /// <param name="year">Upload year.</param>
        /// <param name="month">Upload month.</param>
        /// <param name="content">The content of file.</param>
        /// <param name="appId">Which app it uploaded it.</param>
        /// <returns></returns>
        public async Task <string> SaveFileAsync(int userId, string fileName, string year, string month, byte[] content, EAppType appId)
        {
            // app name
            var appName = appId.ToString().ToLowerInvariant();

            // dir to save this file in
            var dirPath = string.Format("{0}\\{1}\\{2}\\{3}\\{4}\\{5}",
                                        _hostingEnvironment.WebRootPath,
                                        _appSettings.MediaContainerName,
                                        appName,
                                        userId,
                                        year,
                                        month);

            // make sure dir exists
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            // combine dir and filename
            var filePath = Path.Combine(dirPath, fileName);

            // make sure file is unique
            int i = 1;

            while (File.Exists(filePath))
            {
                fileName = fileName.Insert(fileName.LastIndexOf('.'), $"-{i}");
                filePath = Path.Combine(dirPath, fileName);
            }

            // save file to file sys
            using (var targetStream = File.Create(filePath))
                using (MemoryStream stream = new MemoryStream(content))
                {
                    await stream.CopyToAsync(targetStream);
                }

            // returns relative path
            return($"{_appSettings.MediaContainerName}/{appName}/{userId}/{year}/{month}/{fileName}");
        }