예제 #1
0
        private async Task <BaseResult> Create(LectureFile lectureFile)
        {
            var result = new BaseResult();

            var exist = LectureFileRepository.QueryAll().FirstOrDefault(
                x => x.LectureId == lectureFile.LectureId &&
                x.FileId == lectureFile.FileId);

            if (exist != null)
            {
                try
                {
                    exist.IsDeleted = false;
                    await LectureFileRepository.UpdateAsync(exist);
                }
                catch (Exception e)
                {
                    result.Result  = Result.SystemError;
                    result.Message = e.ToString();
                }
            }
            else
            {
                try
                {
                    await LectureFileRepository.InsertAsync(lectureFile);
                }
                catch (Exception e)
                {
                    result.Result  = Result.SystemError;
                    result.Message = e.ToString();
                }
            }
            return(result);
        }
예제 #2
0
        public ActionResult AddLectureFile(decimal lectureId)
        {
            var file   = Request.Files[0];
            var errors = UserFiles.Validate(file, LectureFiles.size, LectureFiles.ext);

            if (errors.Any())
            {
                return(Json(errors.JoinWith(Environment.NewLine)));
            }
            CheckLecturePermission(lectureId);
            LectureFileService.EnableTracking();
            var lectureFile = LectureFileService.FirstOrDefault(x => x.Lecture_ID == lectureId);

            if (lectureFile == null)
            {
                lectureFile            = new LectureFile();
                lectureFile.Lecture_ID = lectureId;
                LectureFileService.InsertAndSubmit(lectureFile);
            }
            file.SaveAs(LectureFiles.GetFile(lectureFile.Id).Path);
            return(Json("ok"));
        }
예제 #3
0
 public static LectureFile ToLectureFile(this LectureFile entity, LectureFile destination)
 {
     return(entity.MapTo(destination));
 }
예제 #4
0
 public static LectureFileItem ToItem(this LectureFile entity)
 {
     return(entity.MapTo <LectureFile, LectureFileItem>());
 }
예제 #5
0
 public static LectureFileModel ToModel(this LectureFile entity)
 {
     return(entity.MapTo <LectureFile, LectureFileModel>());
 }
예제 #6
0
        public IActionResult UploadFile(string lectureId)
        {
            //Get the file from request
            var file = Request.Form.Files[0];

            //Check if the file empty
            if (file == null || file.Length == 0)
            {
                return(BadRequest("fileEmpty"));
            }

            //Should not contains "!!!" because it used by my system for naming
            if (file.Name.Contains("@"))
            {
                return(BadRequest("invalidName"));
            }

            var acceptedFiles = new List <string>()
            {
                "application/vnd.oasis.opendocument.text", "application/octet-stream",
                "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "application/vnd.openxmlformats-officedocument.presentationml.presentation",
                "application/vnd.ms-powerpoint",
                "application/pdf", "application/vnd.geogebra.file", "image/png", "image/jpeg"
            };

            if (!acceptedFiles.Contains(file.ContentType))
            {
                Console.WriteLine($"File Type not supported is: {file.ContentType}");
                return(BadRequest("fileTypeNotSupported"));
            }

            var lecture = _lectureService.GetLecture(lectureId);
            var course  = _courseService.GetCourse(lecture.CourseId);

            var          envRoot            = _environment.ContentRootPath;
            var          root               = "wwwroot";
            const string fileFolder         = "Files";
            const string courseFiles        = "CourseFiles";
            var          courseTarget       = course.Subject + "-" + course.StartDate.Year + "-" + course.Id;
            var          fileName           = "LecNr-" + lecture.Number + "@" + file.Name;
            var          fileSaveTargetPath = Path.Combine(envRoot, root, fileFolder, courseFiles, courseTarget, fileName);
            var          fileGetPath        = "~/" + fileFolder + "/" + courseFiles + "/" + courseTarget + "/" + fileName;

            if (Directory.GetDirectories(envRoot + "/" + root, fileFolder).Length == 0)
            {
                Directory.CreateDirectory(Path.Combine(envRoot + "/" + root + "/" + fileFolder));
            }

            // It was repeated, seems to be wrong, let's try to comment it out
//            if (Directory.GetDirectories(envRoot+"/"+root, fileFolder).Length == 0)
//                Directory.CreateDirectory(Path.Combine(envRoot+"/"+root+"/"+fileFolder));

            if (Directory.GetDirectories(envRoot + "/" + root + "/" + fileFolder, courseFiles).Length == 0)
            {
                Directory.CreateDirectory(Path.Combine(envRoot + "/" + root + "/" + fileFolder + "/" + courseFiles));
            }

            if (Directory.GetDirectories(envRoot + "/" + root + "/" + fileFolder + "/" + courseFiles, courseTarget).Length == 0)
            {
                Directory.CreateDirectory(Path.Combine(envRoot + "/" + root + "/" + fileFolder + "/" + courseFiles + "/" + courseTarget));
            }

            if (Directory.GetFiles(envRoot + "/" + root + "/" + fileFolder + "/" + courseFiles + "/" + courseTarget, fileName).Length !=
                0)
            {
                return(BadRequest("fileExists"));
            }



            try
            {
                using (var stream = new FileStream(fileSaveTargetPath, FileMode.Create))
                {
                    file.CopyToAsync(stream).Wait();
                }

                var courseFile = new LectureFile()
                {
                    Lecture = lecture, Name = file.Name, Path = fileGetPath, RootPath = fileSaveTargetPath
                };

                _context.Add(courseFile);
                _context.SaveChanges();
                return(Ok("fileAddedSuccessfully"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(BadRequest("errorRefresh"));
            }
        }