Advertisement
Guest User

Untitled

a guest
Jun 24th, 2010
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.09 KB | None | 0 0
  1. Imports System
  2. Imports System.IO
  3. Imports ICSharpCode.SharpZipLib.Core
  4. Imports ICSharpCode.SharpZipLib.Zip
  5. Imports System.Drawing
  6.  
  7.  
  8. Public Class Form1
  9.     Public aDir As String = Application.UserAppDataPath
  10.     Public table As DataTable = New DataTable("Tracks")
  11.     Public track_name As DataColumn = New DataColumn("track_name")
  12.     Public full_name As DataColumn = New DataColumn("full_name")
  13.     Public extention As DataColumn = New DataColumn("extention")
  14.     Private tempFolder = createTempFolder()
  15.     Public newRow As DataRow
  16.     Public hhmTracksFileName As String = ""
  17.     Public hhmTracksFilePath As String = ""
  18.     Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  19.     Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Int32, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Int32
  20.     Declare Auto Function InternetConnect Lib "wininet.dll" (ByVal hInternetSession As System.IntPtr, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Int32, ByVal lFlags As Int32, ByVal lContext As System.IntPtr) As System.IntPtr
  21.     Declare Function FtpGetFile Lib "wininet.dll" (ByVal hConnect As IntPtr, ByVal remoteFile As String, ByVal newFile As String, ByVal failIfExists As Boolean, ByVal flagsAndAttributes As Integer, ByVal flags As Integer, ByVal context As IntPtr) As Boolean
  22.     Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hFtpSession As IntPtr, ByVal lpszLocalFile As String, ByVal lpszRemoteFile As String, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean
  23.  
  24.     Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  25.         If (Not System.IO.Directory.Exists(aDir & "/temp")) Then
  26.             System.IO.Directory.CreateDirectory(aDir & "/temp")
  27.         End If
  28.  
  29.         MsgBox("Welkom bij de Hiphopmusic track uploader. Voeg bestanden of gehele mappen toe door de iconen onder aan het programma te gebruiken!")
  30.         track_name.DataType = System.Type.GetType("System.String")
  31.         table.Columns.Add(track_name)
  32.  
  33.         full_name.DataType = System.Type.GetType("System.String")
  34.         table.Columns.Add(full_name)
  35.  
  36.         extention.DataType = System.Type.GetType("System.String")
  37.         table.Columns.Add(extention)
  38.  
  39.         track_list.DataSource = table
  40.         track_list.DisplayMember = table.Columns(0).ColumnName
  41.         track_list.ValueMember = table.Columns(0).ColumnName
  42.     End Sub
  43.     Public Function addFile(ByVal full_name As String)
  44.         Dim temp_track() As String = Split(full_name, "\")
  45.         temp_track = Split(temp_track.Last, ".")
  46.         Dim track As String = ""
  47.         Dim i As Int16 = 1
  48.         For Each ttrack In temp_track
  49.             If Not i = temp_track.Count Then
  50.                 track += ttrack
  51.                 If Not i = (temp_track.Count - 1) Then
  52.                     track += "."
  53.                 End If
  54.             End If
  55.             i = i + 1
  56.         Next
  57.  
  58.  
  59.         newRow = table.NewRow()
  60.         newRow.Item("track_name") = track
  61.         newRow.Item("full_name") = full_name
  62.         newRow.Item("extention") = temp_track.Last
  63.         table.Rows.Add(newRow)
  64.         track_list.SelectedIndex = table.Rows.Count - 1
  65.         Return (False)
  66.     End Function
  67.  
  68.     Private Sub add_track_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add_track.Click
  69.         Dim fileDialog As OpenFileDialog = New OpenFileDialog()
  70.         fileDialog.Filter = "Tracks (*.mp3;*.wma)|*.mp3;*.wma"
  71.         fileDialog.ShowDialog()
  72.         If System.IO.File.Exists(fileDialog.FileName) Then
  73.             addFile(fileDialog.FileName)
  74.         End If
  75.     End Sub
  76.  
  77.     Private Sub add_folder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add_folder.Click
  78.         Dim folderDialog As FolderBrowserDialog = New FolderBrowserDialog()
  79.         folderDialog.ShowDialog()
  80.         Dim folder As String = folderDialog.SelectedPath
  81.         If System.IO.Directory.Exists(folder) Then
  82.             Dim strFileSize As String = ""
  83.             Dim di As New IO.DirectoryInfo(folder)
  84.             Dim aryFi As IO.FileInfo() = di.GetFiles("*.mp3")
  85.             Dim fi As IO.FileInfo
  86.  
  87.             For Each fi In aryFi
  88.                 addFile(fi.FullName)
  89.             Next
  90.  
  91.             strFileSize = ""
  92.             aryFi = di.GetFiles("*.wma")
  93.  
  94.             For Each fi In aryFi
  95.                 addFile(fi.FullName)
  96.             Next
  97.         End If
  98.     End Sub
  99.  
  100.     Private Sub remove_track_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles remove_track.Click
  101.         table.Rows.RemoveAt(track_list.SelectedIndex)
  102.     End Sub
  103.     Public Shared Function FilenameIsOK(ByVal fileNameAndPath As String) As Boolean
  104.         Try
  105.             Dim fi As FileInfo = New FileInfo(fileNameAndPath)
  106.             If fileNameAndPath.Contains("/") Then
  107.                 Return False
  108.             ElseIf fileNameAndPath.Contains("\") Then
  109.                 Return False
  110.             Else
  111.                 Return True
  112.             End If
  113.         Catch ex As Exception
  114.             Return False
  115.         End Try
  116.     End Function
  117.     Private Sub rename_track_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rename_track.Click
  118.         If Not track_list.SelectedValue = "" Then
  119.             Dim input As String = InputBox("Vul hieronder de nieuwe titel van de track in", "Hernoem Track", track_list.SelectedValue)
  120.             If Not input = "" Then
  121.                 If FilenameIsOK(input & "." & table(track_list.SelectedIndex)("extention")) Then
  122.                     table(track_list.SelectedIndex)("track_name") = input
  123.                 Else
  124.                     MsgBox("De naam is niet geldig. Er mag geen gebruik worden gemaakt van de tekens: / \ : * ?  < >")
  125.                 End If
  126.             Else
  127.                 MsgBox("U moet wel wat invullen")
  128.             End If
  129.         Else
  130.             MsgBox("U moet wel wat selecteren")
  131.         End If
  132.     End Sub
  133.  
  134.     Private Sub track_up_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles track_up.Click
  135.         Dim selected As Integer = track_list.SelectedIndex
  136.         If selected = 0 Then
  137.             MsgBox("De track staat al bovenaan")
  138.         Else
  139.             Dim track As DataRow = table(selected)
  140.             newRow = table.NewRow()
  141.             newRow.Item("track_name") = track("track_name")
  142.             newRow.Item("full_name") = track("full_name")
  143.             newRow.Item("extention") = track("extention")
  144.             table.Rows.RemoveAt(selected)
  145.             table.Rows.InsertAt(newRow, (selected - 1))
  146.             track_list.SelectedIndex = selected - 1
  147.         End If
  148.     End Sub
  149.  
  150.     Private Sub track_down_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles track_down.Click
  151.         Dim selected As Integer = track_list.SelectedIndex
  152.         If selected = (table.Rows.Count - 1) Then
  153.             MsgBox("De track staat al onderaan")
  154.         Else
  155.             Dim track As DataRow = table(selected)
  156.             newRow = table.NewRow()
  157.             newRow.Item("track_name") = track("track_name")
  158.             newRow.Item("full_name") = track("full_name")
  159.             newRow.Item("extention") = track("extention")
  160.             table.Rows.RemoveAt(selected)
  161.             table.Rows.InsertAt(newRow, (selected + 1))
  162.             track_list.SelectedIndex = selected + 1
  163.         End If
  164.     End Sub
  165.     Private Function createTempFolder()
  166.         Dim randomClass As Random = New Random()
  167.         Dim uniqid As String = randomClass.Next(1, 100000) & randomClass.Next(1, 100000) & randomClass.Next(1, 100000)
  168.         If (Not System.IO.Directory.Exists(aDir & "/temp/" & uniqid)) Then
  169.             System.IO.Directory.CreateDirectory(aDir & "/temp/" & uniqid)
  170.         Else
  171.             Return createTempFolder()
  172.         End If
  173.  
  174.         Return aDir & "/temp/" & uniqid
  175.     End Function
  176.  
  177.     Private Sub make_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles make.Click
  178.         hhmTracksFileName = InputBox("Vul de naam in van het bestand dat u wilt maken", "Bestand maken...")
  179.         If Not FilenameIsOK(hhmTracksFileName) Then
  180.             MsgBox("De bestandsnaam is niet geldig.")
  181.         Else
  182.             hhmTracksFilePath = aDir & "/temp/" & hhmTracksFileName & ".hhm-tracks"
  183.             Dim X As MsgBoxResult = MsgBox("U staat op het punt om een HHM-tracks bestand te maken. Het maken van dit bestand kan lang duren. U dient het programma in de tussentijd niet af te sluiten. Weet u zeker dat u het procces wilt starten?", 3)
  184.             If X = MsgBoxResult.Yes Then
  185.                 info_label2.Text = "Bezig met maken van bestand..."
  186.  
  187.  
  188.                 UseWaitCursor = True
  189.  
  190.                 track_list.Enabled = False
  191.                 ToolStrip1.Enabled = False
  192.                 Me.Text = "Hiphopmusic track uploader v1.0 Beta | Maken van bestand... 0%"
  193.  
  194.                 BackgroundWorker.RunWorkerAsync()
  195.  
  196.  
  197.  
  198.             End If
  199.         End If
  200.     End Sub
  201.  
  202.     Private Sub BackgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
  203.         Dim i As Double = 1
  204.         BackgroundWorker.ReportProgress(10)
  205.         Dim pl As Double = 30 / (table.Rows.Count - 1)
  206.         Dim apl As Double = 10
  207.         For Each track In table.Rows
  208.             Dim newFileName As String = tempFolder & "\" & i & "-" & track("track_name") & "." & track("extention")
  209.             My.Computer.FileSystem.CopyFile(track("full_name"), newFileName)
  210.             i = i + 1
  211.             BackgroundWorker.ReportProgress(apl + pl)
  212.             apl = apl + pl
  213.         Next
  214.         If (File.Exists(hhmTracksFilePath)) Then
  215.             File.Delete(hhmTracksFilePath)
  216.         End If
  217.         Dim strmZipOutputStream As ZipOutputStream
  218.         Dim astrFileNames() As String = Directory.GetFiles(tempFolder)
  219.         strmZipOutputStream = New ZipOutputStream(File.Create(hhmTracksFilePath))
  220.         strmZipOutputStream.SetLevel(9)
  221.         Dim strFile As String
  222.         Dim abyBuffer(4096) As Byte
  223.         BackgroundWorker.ReportProgress(apl + 5)
  224.         pl = 45 / astrFileNames.Length
  225.         For Each strFile In astrFileNames
  226.             Dim strmFile As FileStream = File.OpenRead(strFile)
  227.             Try
  228.  
  229.                 Dim objZipEntry As ZipEntry = New ZipEntry(Path.GetFileName(strFile))
  230.  
  231.                 objZipEntry.DateTime = DateTime.Now
  232.                 objZipEntry.Size = strmFile.Length
  233.  
  234.                 strmZipOutputStream.PutNextEntry(objZipEntry)
  235.                 StreamUtils.Copy(strmFile, strmZipOutputStream, abyBuffer)
  236.             Finally
  237.                 strmFile.Close()
  238.             End Try
  239.             BackgroundWorker.ReportProgress(apl + pl)
  240.             apl = apl + pl
  241.         Next
  242.         strmZipOutputStream.Finish()
  243.         strmZipOutputStream.Close()
  244.  
  245.         System.IO.Directory.Delete(tempFolder, True)
  246.         BackgroundWorker.ReportProgress(100)
  247.     End Sub
  248.  
  249.     Private Sub BackgroundWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker.ProgressChanged
  250.         If e.ProgressPercentage < 100 Then
  251.             ToolStripProgressBar1.ProgressBar.Value = e.ProgressPercentage
  252.         Else
  253.             ToolStripProgressBar1.ProgressBar.Value = 100
  254.         End If
  255.         Me.Text = "Hiphopmusic track uploader v1.0 Beta | Maken van bestand... " & e.ProgressPercentage & "%"
  256.     End Sub
  257.  
  258.     Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker.RunWorkerCompleted
  259.         info_label2.Text = "Bezig met uploaden van bestand..."
  260.         Me.Text = "Hiphopmusic track uploader v1.0 Beta | Uploaden van bestand..."
  261.         ToolStripProgressBar1.ProgressBar.Style = 2
  262.         ftpWorker.RunWorkerAsync()
  263.     End Sub
  264.  
  265.     Private Sub ftpWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles ftpWorker.DoWork
  266.  
  267.  
  268.         Dim ftpOpen = InternetOpen("FTP", 0, vbNullString, vbNullString, 0)
  269.         Dim ftpConnection = InternetConnect(ftpOpen, "", 0, "", "", 1, 0, 0)
  270.         ftpWorker.ReportProgress(20)
  271.         FtpPutFile(ftpConnection, hhmTracksFilePath, "/temp-tracks-folder/" & hhmTracksFileName & ".hhm-tracks", 1, 0)
  272.     End Sub
  273.  
  274.  
  275.     Private Sub ftpWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles ftpWorker.ProgressChanged
  276.         Me.Text = "Hiphopmusic track uploader v1.0 Beta | Uploaden van bestand... "
  277.     End Sub
  278.  
  279.     Private Sub ftpWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles ftpWorker.RunWorkerCompleted
  280.         System.IO.File.Delete(hhmTracksFilePath)
  281.         MsgBox("Het bestand is geupload. U kunt nu op de website de tracks toevoegen.")
  282.         Me.Close()
  283.     End Sub
  284. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement