public HttpResponseMessage Create(CPlaylistInfo playlistInfo) { HttpResponseMessage response; try { CPlaylist newPlaylist = new CPlaylist(playlistInfo); Int32 created = _playlistContext.Create(newPlaylist); if (created < 1) { response = Request.CreateResponse(HttpStatusCode.BadRequest, "Couldn't create playlist"); } else { response = Request.CreateResponse(HttpStatusCode.OK, $"Created playlist {playlistInfo.Name}!"); } return(response); } catch (ContextException e) { HttpContext.Current.Response.StatusCode = (Int32)HttpStatusCode.BadRequest; throw new ContextException(e.Message, e); } }
public Int32 Update(CPlaylist playlist) { SqlConnection myConnection = new SqlConnection(); try { using (myConnection) { myConnection.ConnectionString = ConnectionContext.GetConnectionString(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = $"UPDATE Playlists SET " + $"Name=@playlistName, IsPublic=@isPublic" + $" WHERE Id=@playlistId"; sqlCmd.Connection = myConnection; sqlCmd.Parameters.AddWithValue("@playlistName", playlist.Name); sqlCmd.Parameters.AddWithValue("@playlistId", playlist.Guid); sqlCmd.Parameters.AddWithValue("@isPublic", playlist.IsPublic); myConnection.Open(); Int32 rowUpdated = sqlCmd.ExecuteNonQuery(); return(rowUpdated); } } catch (Exception) { throw; } }
public HttpResponseMessage Update(CPlaylistInfo playlistInfo) { HttpResponseMessage response; try { CPlaylist updatePlaylist = new CPlaylist(playlistInfo); Int32 updated = _playlistContext.Update(updatePlaylist); if (updated < 1) { response = Request.CreateResponse(HttpStatusCode.BadRequest, $"Couldn't update playlist {playlistInfo.Name}"); } else { response = Request.CreateResponse(HttpStatusCode.OK, $"Updated playlist {playlistInfo.Name}!"); } return(response); } catch (Exception e)// in case program crashes? { HttpContext.Current.Response.StatusCode = (Int32)HttpStatusCode.BadRequest; throw new ContextException(e.Message, e); } }
public Int32 Create(CPlaylist playlist) { try { SqlConnection myConnection = new SqlConnection(); using (myConnection) { myConnection.ConnectionString = ConnectionContext.GetConnectionString(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "INSERT INTO Playlists (Id,Name,UserId,IsPublic) " + $"Values (@Id, @Name,@UserId,@IsPublic)"; sqlCmd.Connection = myConnection; sqlCmd.Parameters.AddWithValue("@Id", playlist.Guid); sqlCmd.Parameters.AddWithValue("@Name", playlist.Name); sqlCmd.Parameters.AddWithValue("@UserId", playlist.UserId); sqlCmd.Parameters.AddWithValue("@IsPublic", playlist.IsPublic); myConnection.Open(); Int32 rowsInserted = sqlCmd.ExecuteNonQuery(); myConnection.Close(); return(rowsInserted); } } catch (Exception e) { throw new ContextException("Cannot add given playlist!", e); } }
private CPlaylistVM(CPlaylist playlist, CPlaylistVM parent) { _parent = parent; _playlist = playlist; UpdateChildren(); }
// get all public playlists for given user (beside his own) public IEnumerable <CPlaylist> GetAllPublic(Guid userId) { SqlConnection myConnection = new SqlConnection(); using (myConnection) { SqlDataReader reader = null; myConnection.ConnectionString = ConnectionContext.GetConnectionString(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = "SELECT * FROM Playlists WHERE IsPublic=@isPublic AND UserId!=@userId"; sqlCmd.Connection = myConnection; sqlCmd.Parameters.AddWithValue("@isPublic", true); sqlCmd.Parameters.AddWithValue("@userId", userId); myConnection.Open(); reader = sqlCmd.ExecuteReader(); List <CPlaylist> playlists = new List <CPlaylist>(); while (reader.Read()) { CPlaylist playlist = new CPlaylist(); playlist.Guid = (Guid)(reader.GetValue(0)); playlist.Name = reader.GetValue(1).ToString(); playlist.UserId = (Guid)reader.GetValue(2); playlist.IsPublic = (bool)reader.GetValue(3); playlists.Add(playlist); } //myConnection.Close(); return(playlists); } }
public MainWindow() { InitializeComponent(); CPlaylist playlist = CPlaylistBroker.LoadPlaylists(App.iTunes); mwv = new MainWindowVM(playlist); DataContext = mwv; }
public MainWindowVM(CPlaylist rootPlaylist) { _rootPlaylist = new CPlaylistVM(rootPlaylist); _playlists = new ObservableCollection <CPlaylistVM>(_rootPlaylist.Children); //_playlists = new ObservableCollection<CPlaylistVM>( // new CPlaylistVM[] // { // _rootPlaylist // }); InitCommands(); }
public HttpResponseMessage GetById(Guid playlistId) { HttpResponseMessage response; CPlaylistInfo playlistInfo = new CPlaylistInfo(); try { CPlaylist playlist = _playlistContext.GetById(playlistId); playlistInfo = playlist.ToCPlaylistInfo(); response = Request.CreateResponse(HttpStatusCode.OK, playlistInfo); return(response); } catch (Exception e)// in case program crashes? { HttpContext.Current.Response.StatusCode = (Int32)HttpStatusCode.BadRequest; throw new ContextException(e.Message, e); } }
public IEnumerable <CPlaylist> GetByUserId(Guid userId) { try { SqlConnection myConnection = new SqlConnection(); using (myConnection) { SqlDataReader reader = null; myConnection.ConnectionString = ConnectionContext.GetConnectionString(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = $"SELECT * FROM Playlists WHERE UserId=@userId"; sqlCmd.Connection = myConnection; sqlCmd.Parameters.AddWithValue("@userId", userId); myConnection.Open(); reader = sqlCmd.ExecuteReader(); List <CPlaylist> playlists = new List <CPlaylist>(); while (reader.Read()) { CPlaylist playlist = new CPlaylist(); playlist.Guid = (Guid)(reader.GetValue(0)); playlist.Name = reader.GetValue(1).ToString(); playlist.UserId = (Guid)reader.GetValue(2); playlist.IsPublic = (bool)reader.GetValue(3); playlists.Add(playlist); } return(playlists); } } catch (Exception e) { throw new ContextException("Cannot find playlist!", e); } }
protected void _AddPlaylist(CPlaylist pls, String key = null) { _AddElement(_Playlists.Add(pls, key), EType.Playlist); }
public HttpResponseMessage Register(CUserInfo userInfo) { HttpResponseMessage response; if (userInfo == null) { response = Request.CreateResponse(HttpStatusCode.BadRequest, "User has no data to register!"); return(response); } try { // check if such user already exists in context CUser checkCUser = _userContext.GetByName(userInfo.Name); if (checkCUser.Name != null) { response = Request.CreateResponse(HttpStatusCode.BadRequest, "Such user already exists!"); return(response); } // check if such email is already taken by someone else CUser checkEmail = _userContext.GetByName(userInfo.Email); if (checkEmail.Email != null) { response = Request.CreateResponse(HttpStatusCode.BadRequest, "Such email is already taken!"); return(response); } // in CUser CTOR new Guid and passwordHash is generated from given password CUser newUser = new CUser(Guid.NewGuid(), userInfo.Name, userInfo.Password, userInfo.Email); // Insert new user in DB Int32 rowsInserted = _userContext.Create(newUser); // Create 'default' playlist for user files CPlaylist defaultPlaylist = new CPlaylist(Guid.NewGuid(), "default", newUser.Guid, false); _playlistContext.Create(defaultPlaylist); // Create new folder for user files DirectoryInfo directoryInfo = new DirectoryInfo(System.Web.Hosting.HostingEnvironment.MapPath($@"~/App_Data")); if (!Directory.Exists(directoryInfo.ToString() + "\\UserFiles")) { Directory.CreateDirectory(directoryInfo.ToString() + "\\UserFiles"); } // create directory for user files string fullPath = System.Web.Hosting.HostingEnvironment.MapPath($@"~/App_Data/UserFiles/{newUser.Name}"); Directory.CreateDirectory(fullPath); // create directory for thumbnails of the videos string thumbnailsPath = System.Web.Hosting.HostingEnvironment.MapPath($@"~/App_Data/UserFiles/{newUser.Name}/Thumbnails"); Directory.CreateDirectory(thumbnailsPath); userInfo = newUser.ToCUserInfo(); response = Request.CreateResponse(HttpStatusCode.OK, userInfo); return(response); } catch (Exception e) { //todo: log HttpContext.Current.Response.StatusCode = (Int32)HttpStatusCode.BadRequest; throw new ContextException(e.Message, e); } }
public static CPlaylist LoadPlaylists(iTunesApp itunes) { CPlaylist retPlaylist = new CPlaylist(null); IITSourceCollection sources = itunes.Sources; IITPlaylistCollection playlists = null; IITSource plSource = null; foreach (IITSource source in sources) { if (source.Kind == ITSourceKind.ITSourceKindLibrary) { plSource = source; playlists = source.Playlists; break; } } foreach (IITPlaylist pl in playlists) { if (pl.Kind != ITPlaylistKind.ITPlaylistKindUser) { continue; } try { IITUserPlaylist upl = (IITUserPlaylist)pl; if (upl.Smart) { continue; } if (upl.SpecialKind == ITUserPlaylistSpecialKind.ITUserPlaylistSpecialKindPodcasts) { continue; } string strDir = string.Empty; Stack <IITUserPlaylist> parentStack = new Stack <IITUserPlaylist>(); IITUserPlaylist uplp = upl.get_Parent(); if (uplp != null) { parentStack.Push(uplp); IITUserPlaylist uplc = uplp; do { uplp = uplc.get_Parent(); if (uplp != null) { parentStack.Push(uplp); uplc = uplp; } } while (uplp != null); } CPlaylist parentPL = retPlaylist; bool bFoundLeaf = false; do { uplp = (parentStack.Count > 0) ? parentStack.Pop() : null; if (uplp == null) { bFoundLeaf = true; } else { CPlaylist childPL = null; foreach (var item in parentPL.Children) { if (item.Name == uplp.Name) { childPL = item; break; } } if (childPL != null) { parentPL = childPL; } else { bFoundLeaf = true; } } } while (!bFoundLeaf); while (uplp != null) { CPlaylist plChild = new CPlaylist(uplp, true); parentPL.Children.Add(plChild); parentPL = plChild; uplp = (parentStack.Count > 0) ? parentStack.Pop() : null; } parentPL.Children.Add(new CPlaylist(upl)); } catch (Exception ex) { Debug.WriteLine(ex); } } return(retPlaylist); }
public CPlaylistVM(CPlaylist playlist) : this(playlist, null) { }
//[Authorize] public async Task <HttpResponseMessage> UploadChunkAsync() { SqlConnection myConnection = new SqlConnection(); SqlTransaction myTransaction; using (myConnection) { myConnection.ConnectionString = ConnectionContext.GetConnectionString(); myConnection.Open(); // TRANSACTION - in case of SQL error or File write to disk error - rollback will be awailable myTransaction = myConnection.BeginTransaction("UploadFileTransaction"); try { SetUserDetails(); HttpResponseMessage response; String responseMsg; // check if such hash has record in DB. CFile newFile = _fileContext.GetByHash(fileInfo.Hash, myConnection, myTransaction); // if such hash and exist in DB and user names match if (!newFile.Guid.Equals(Guid.Empty) && newFile.UserId.Equals(user.Guid)) { response = Request.CreateResponse(HttpStatusCode.BadRequest); response.ReasonPhrase = $"Another file {newFile.Name} with same hash than{fileInfo.Name} already exists on Media Server!"; return(response); } // check if such file name has record in DB. newFile = _fileContext.GetByFileName(fileInfo.Name, user.Guid, myConnection, myTransaction); // if file with such name already exists in DB if (!newFile.Guid.Equals(Guid.Empty)) { // file exits, it has hash (so it is 100% loaded), but hash is different, which means this is the other file! if (newFile.Hash != null) { response = Request.CreateResponse(HttpStatusCode.BadRequest); response.ReasonPhrase = $"Another file {newFile.Name} with same hash than{fileInfo.Name} already exists on Media Server!"; return(response); } else { // file exists, but it has no hash, so it hasn't been 100% loaded - continue loading } } // fileName not found in DB - create new file record else { // add record to DB with file info newFile = new CFile(fileInfo, user, userFolder); // create record in DB about new file Int32 added = _fileContext.Create(newFile, myConnection, myTransaction); if (added == 0) { response = Request.CreateResponse(HttpStatusCode.BadRequest, $"Cannot add file to DB!"); return(response); } // SQL generates primary field values, so get created file back with proper Guid newFile = _fileContext.GetByFileName(newFile.Name, user.Guid, myConnection, myTransaction); // add file to all playlists specified by user. Default playlist is 'ON' if no other playlists are specified. if (fileInfo.playlists.Count == 0) { CPlaylist playlist = _playlistContext.GetByName("default", user.Guid); _fileContext.AddToPlaylist(newFile.Guid, playlist.Guid, myConnection, myTransaction); } else { // add files to other playlists specified by user foreach (CPlaylistInfo playlistInfo in fileInfo.playlists) { CPlaylist playlist = new CPlaylist(playlistInfo); _fileContext.AddToPlaylist(newFile.Guid, playlist.Guid, myConnection, myTransaction); } } } // After all checks on existing files, continue loading given chunk of byte data //todo: upload directly to memory stream or to file stream? MemoryStream ms = new MemoryStream(new byte[chunkSize], true); using (ms) { await Request.Content.CopyToAsync(ms); // check if all bytes from the chunk have been loaded (no disconnect or program crash happened during load) if (ms.Length == chunkSize) { using (FileStream fs = new FileStream (newFile.Path + newFile.Name, FileMode.Append, FileAccess.Write, FileShare.None, chunkSize, useAsync: true)) { ms.WriteTo(fs); } } else { // couldn't upload file chunk - so rollback record from DB myTransaction.Rollback(); response = Request.CreateResponse(HttpStatusCode.OK, $"Chunk has not been uploaded correctly!"); return(response); } } responseMsg = $"Chunk for file:{newFile.Name} successfully uploaded!"; // calculate Hash only if file has been loaded 100% if (isLastChunk) { // calculate Hash only if file has been loaded 100% await Task.Factory.StartNew(() => HashFile(newFile)).ContinueWith(delegate { // calculate Thumbnail for video only if file has been loaded 100% CreateThumbnail(newFile); }); responseMsg = $"File successfully uploaded!"; } // update file data in DB: hash, fileSize, ... newFile.Size += chunkSize; _fileContext.Update(newFile, myConnection, myTransaction); // if all went OK, commit records to DB myTransaction.Commit(); // return response if success response = Request.CreateResponse(HttpStatusCode.OK, responseMsg); return(response); } catch (Exception e) { myTransaction.Rollback(); HttpContext.Current.Response.StatusCode = (Int32)HttpStatusCode.BadRequest; throw new FileUploadException(e.Message, e); } } }