Exemple #1
0
        public int SaveUploadedFilePath(FileDTO fileDTO, int moduleId = -1, int hwId = -1)
        {
            if (moduleId > 0)
            {
                var module = _moduleRepo.Get(m => m.ModuleId.Equals(moduleId));
                if (module == null)
                    return -1;
                var file = Mapper.Map<File>(fileDTO);

                file.Module = module;

                _fileRepo.Add(file);
                _fileRepo.SaveChanges();

                module.Files.Add(file);
                _moduleRepo.SaveChanges();
                return file.Id;
            }

            if (hwId > 0)
            {
                var hw = _hwRepo.Get(h => h.HomeworkId.Equals(hwId));
                if (hw == null)
                    return -1;

                var file = Mapper.Map<File>(fileDTO);

                file.Module = hw.Module;
                
                _fileRepo.Add(file);
                _fileRepo.SaveChanges();
                return file.Id;
            }
            return -1;
        }
Exemple #2
0
        public static async Task<int> UploadFile(string token, byte[] contentFile, IFileProvider fileProvider,
            string filename, int moduleId = -1, int hwId = -1)
        {

            string username = TokenHelper.GetFromToken(token, "username");
            string role = TokenHelper.GetFromToken(token, "role");

            string path = "/" + role + "/" + username + "/" + filename;

            var key = WebConfigurationManager.AppSettings["DropboxToken"];
            var dbx = new DropboxClient(key);

            FileType fileType;
            var lastOrDefault = filename.Split('.').LastOrDefault();
            if (lastOrDefault != null)
                if (Enum.TryParse(lastOrDefault, out fileType))
                {
                    using (var memoryStream = new MemoryStream(contentFile))
                    {
                        try
                        {
                            await dbx.Files.UploadAsync(
                                path,
                                WriteMode.Add.Instance.AsAdd,
                                body: memoryStream);
                            Logger.Logger.Instance.LogAction(LoggerHelper.GetActionString(username, "Upload File"));
                        }
                        catch (Exception ex)
                        {
                            Logger.Logger.Instance.LogError(ex);
                            throw;
                        }
                        var input = new FileDTO
                        {
                            Path = path,
                            FileType = fileType,
                            FileName = filename
                        };
                        var fileId = fileProvider.SaveUploadedFilePath(input, moduleId, hwId);
                        if (fileId < 0)
                            throw new Exception("Cannot save file path");
                        else
                            return fileId;
                    }
                }
                else
                    throw new Exception("File tye not supported");
            else
                throw new Exception("Not a file type");
        }