/// <summary>
        /// <para><b>EN: </b>Save uploaded IFormFile file to server. Target Path will be : ".../wwwroot/Media Library/Image Library/<paramref name="entity"></paramref>.Id"</para>
        /// <para><b>TR: </b>Yüklenen IFormFile dosyasını sunucuya kaydedin. Hedef Yol: "... / wwwroot / Media Library / Image Library / <paramref name =" entity "> </paramref> .Id" olacaktır</para>
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="file"> Uploaded file in entity. </param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static async Task <string> SaveImageToServerAsync <TEntity, TKey>(this IFormFile file, TEntity entity) where TEntity : AuditableEntity <AppUser, Guid, TKey>
            where TKey : struct, IEquatable <TKey>
        {
            string basePath = GlobalConstants.ImageLibraryPath;

            FormFileOperations.FilesFolderNameCreator imagesFolderNameCreator = CreateImageFolderNameFromDTO;

            string propertyName = "Id";

            int maxFileLength = 14000000;

            var allowedFileExtensions = GlobalConstants.AllowedFileExtensions.Find(i => i.FileType == FileType.Image.ToString()).AllowedExtensions;

            var validationResult = file.ValidateFile(maxFileLength, allowedFileExtensions, FileType.Image);

            switch (validationResult)
            {
            case FileValidationResult.Valid:
                break;

            case FileValidationResult.FileSizeTooBig:
                // Get length of file in bytes
                long fileSizeInBytes = file.Length;
                // Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
                double fileSizeInKB = fileSizeInBytes / 1024;
                // Convert the KB to MegaBytes (1 MB = 1024 KBytes)
                double fileSizeInMB = fileSizeInKB / 1024;
                throw new MilvaUserFriendlyException("FileIsTooBigMessage", fileSizeInMB.ToString("0.#"));

            case FileValidationResult.InvalidFileExtension:
                var stringBuilder = new StringBuilder();
                throw new MilvaUserFriendlyException("UnsupportedFileTypeMessage", stringBuilder.AppendJoin(", ", allowedFileExtensions));

            case FileValidationResult.NullFile:
                return("");
            }

            var path = await file.SaveFileToPathAsync(entity, basePath, imagesFolderNameCreator, propertyName).ConfigureAwait(false);

            await file.OpenReadStream().DisposeAsync().ConfigureAwait(false);

            return(path);
        }
Exemple #2
0
        /// <summary>
        /// Allows the student to turn in the assignment.
        /// </summary>
        /// <param name="submitAssignment"></param>
        /// <returns></returns>
        public async Task <string> SubmitAssignmentAsync(SubmitAssignmentDTO submitAssignment)
        {
            var currentStudent = await _studentRepository.GetFirstOrDefaultAsync(i => i.AppUser.UserName == _loggedUser).ConfigureAwait(false);

            string basePath = GlobalConstants.DocumentLibraryPath;

            FormFileOperations.FilesFolderNameCreator assignmentFolderNameCreator = (Type type) => type.Name + "Assignment";

            var maxFileLength = 140000000;

            var allowedFileExtensions = GlobalConstants.AllowedFileExtensions.Find(i => i.FileType == FileType.Compressed.ToString()).AllowedExtensions;

            var validationResult = submitAssignment.Assignment.ValidateFile(maxFileLength, allowedFileExtensions, FileType.Compressed);

            var path = await submitAssignment.Assignment.SaveFileToPathAsync(submitAssignment, basePath, assignmentFolderNameCreator, "Id").ConfigureAwait(false);

            await submitAssignment.Assignment.OpenReadStream().DisposeAsync().ConfigureAwait(false);

            await _milvaMailSender.MilvaSendMailAsync(currentStudent.Mentor.AppUser.Email, Helpers.Enums.MailSubject.Information, currentStudent.Name + currentStudent.Surname + " isimli öğrencinin ödevini gönderdi.");

            return(path);
        }