Skip to content

Commit 03e3a07

Browse files
committed
2024.2.22.1
Feed: add the ability to update file location when moving media Automation: make the automation file relative
1 parent d283d9c commit 03e3a07

9 files changed

Lines changed: 200 additions & 43 deletions

File tree

SCrawler/Download/Automation/Scheduler.vb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ Namespace DownloadObjects
3838
End Property
3939
Friend Sub New()
4040
Plans = New List(Of AutoDownloader)
41-
File = Settings.AutomationFile.Value.IfNullOrEmpty(FileDefault)
41+
Dim sFolder As SFile = SettingsFolderName.CSFileP
42+
File = New SFile(Settings.AutomationFile.Value.IfNullOrEmpty(FileDefault.ToString))
43+
If File.Path.IsEmptyString OrElse Not File.Path.StartsWith(sFolder.Path) Then
44+
Dim updateSetting As Boolean = File.Path.IsEmptyString OrElse Not File.Path.StartsWith(sFolder.CutPath.Path)
45+
File.Path = sFolder.Path
46+
If File.Exists And updateSetting Then Settings.AutomationFile.Value = File.File
47+
End If
4248
If Not File.Exists Then File = FileDefault
4349
Reset(File, True)
4450
End Sub

SCrawler/Download/Automation/SchedulerEditorForm.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ Namespace DownloadObjects
279279
If Not Settings.Automation.File = f AndAlso Settings.Automation.Reset(f, False) Then
280280
Settings.Automation.File = f
281281
If selectedName = defName Then
282-
Settings.AutomationFile.Value = Nothing
282+
Settings.AutomationFile.Value = String.Empty
283283
Else
284-
Settings.AutomationFile.Value = f
284+
Settings.AutomationFile.Value = f.File
285285
End If
286286
PauseArr.UpdatePauseButtons()
287287
Refill()

SCrawler/Download/Feed/DownloadFeedForm.vb

Lines changed: 123 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Namespace DownloadObjects
180180
DataList.Clear()
181181
End Sub
182182
Private Sub DownloadFeedForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
183-
If e.KeyCode = Keys.F5 Then RefillList0() : e.Handled = True
183+
If e.KeyCode = Keys.F5 Then RefillList() : e.Handled = True
184184
End Sub
185185
#End Region
186186
#Region "Feeds handlers"
@@ -340,19 +340,19 @@ Namespace DownloadObjects
340340
MyRange.HandlersSuspended = True
341341
MyRange.Limit = c
342342
MyRange.HandlersSuspended = False
343-
If Not MyDefs.Initializing Then RefillList0()
343+
If Not MyDefs.Initializing Then RefillList()
344344
End With
345345
End Sub
346346
#End Region
347347
#Region "Refill"
348-
Private Sub RefillList0(Optional ByVal RememberPosition As Boolean? = Nothing)
348+
Private Overloads Sub RefillList(Optional ByVal RememberPosition As Boolean? = Nothing)
349349
If IsSession Then
350350
RefillList(FeedMode = FeedModes.Current, If(RememberPosition, True))
351351
Else
352352
RefillSpecialFeedsData()
353353
End If
354354
End Sub
355-
Private Sub RefillList(Optional ByVal RefillDataList As Boolean = True, Optional ByVal RememberPosition As Boolean = False)
355+
Private Overloads Sub RefillList(ByVal RefillDataList As Boolean, ByVal RememberPosition As Boolean)
356356
DataPopulated = False
357357
Dim rIndx% = -1
358358
If RememberPosition Then rIndx = MyRange.CurrentIndex
@@ -493,39 +493,136 @@ Namespace DownloadObjects
493493
End Sub
494494
#End Region
495495
Private Sub BTT_COPY_MOVE_TO_Click(sender As Object, e As EventArgs) Handles BTT_COPY_TO.Click, BTT_MOVE_TO.Click
496+
MoveCopyFiles(True, sender, Nothing, Nothing)
497+
End Sub
498+
Private Function MoveCopyFiles(ByVal IsInternal As Boolean, ByVal Sender As Object, ByVal NewDestination As SFile, ByVal FeedMediaFile As SFile) As Boolean
496499
Const MsgTitle$ = "Copy/Move checked files"
497500
Try
498-
Dim isCopy As Boolean = sender Is BTT_COPY_TO
501+
Dim isCopy As Boolean = Not Sender Is Nothing AndAlso Sender Is BTT_COPY_TO
499502
Dim dest As SFile = Nothing
500-
Dim ff As SFile, df As SFile
503+
Dim ff As SFile = Nothing, df As SFile
501504
Dim files As IEnumerable(Of SFile) = Nothing
505+
Dim mm As UserMediaD
506+
Dim mm_data As API.Base.UserMedia
507+
Dim indx%
508+
Dim renameExisting As Boolean = False
509+
Dim downloaderFilesUpdated As Boolean = False
510+
Dim eFiles As IEnumerable(Of SFile)
511+
Dim finder As Predicate(Of UserMediaD) = Function(media) media.Data.File = ff
512+
Dim x As XmlFile
513+
Dim sessionData As New List(Of UserMediaD)
514+
Dim sesFile As SFile
515+
Dim sesFilesReplaced As Boolean = False
516+
Dim filesReplace As New List(Of KeyValuePair(Of SFile, SFile))
517+
Dim updateFileLocations As Boolean = Settings.FeedUpdateFileLocationOnMove
518+
Dim result As Boolean = False
502519

503-
With GetCheckedMedia()
504-
If .ListExists Then files = .Select(Function(m) m.Data.File)
505-
End With
520+
If FeedMediaFile.IsEmptyString Then
521+
With GetCheckedMedia()
522+
If .ListExists Then files = .Select(Function(m) m.Data.File)
523+
End With
524+
Else
525+
files = {FeedMediaFile}
526+
End If
506527
If files.ListExists Then
507-
Using f As New FeedCopyToForm(files, isCopy)
508-
f.ShowDialog()
509-
If f.DialogResult = DialogResult.OK Then dest = f.Destination
510-
End Using
528+
If NewDestination.IsEmptyString Then
529+
Using f As New FeedCopyToForm(files, isCopy)
530+
f.ShowDialog()
531+
If f.DialogResult = DialogResult.OK Then dest = f.Destination
532+
End Using
533+
Else
534+
dest = NewDestination
535+
End If
511536
If Not dest.IsEmptyString Then
537+
If Not isCopy Then
538+
eFiles = files.Where(Function(ByVal fff As SFile) As Boolean
539+
fff.Path = dest
540+
Return fff.Exists
541+
End Function)
542+
If eFiles.ListExists Then _
543+
renameExisting = MsgBoxE(New MMessage("The following files already exist at the destination. " &
544+
"Do you still want to move them? These files will be renamed and moved." & vbCr &
545+
$"Destination: {dest.PathWithSeparator}{vbCr}{vbCr}" &
546+
eFiles.ListToString(vbCr), MsgTitle, {"Move", "Cancel"}, vbExclamation)) = 0
547+
548+
End If
512549
For Each ff In files
513550
If Not ff.IsEmptyString Then
514551
df = ff
515552
df.Path = dest.Path
516-
If isCopy Then ff.Copy(df) Else SFile.Move(ff, df)
553+
If isCopy Then
554+
If ff.Copy(df) Then result = True
555+
Else
556+
If df.Exists And renameExisting Then df = SFile.IndexReindex(df,,,, New ErrorsDescriber(False, False, False, df))
557+
If SFile.Move(ff, df) Then
558+
result = True
559+
If updateFileLocations Then
560+
filesReplace.Add(New KeyValuePair(Of SFile, SFile)(ff, df))
561+
indx = Downloader.Files.FindIndex(finder)
562+
If indx >= 0 Then
563+
mm = Downloader.Files(indx)
564+
mm_data = mm.Data
565+
mm_data.File = df
566+
mm = New UserMediaD(mm_data, mm.User, mm.Session, mm.Date)
567+
Downloader.Files(indx) = mm
568+
downloaderFilesUpdated = True
569+
End If
570+
End If
571+
End If
572+
End If
517573
End If
518574
Next
519-
If Not isCopy Then RefillList0()
520-
MsgBoxE({$"The following files were copied to{vbCr}{dest}{vbCr}{vbCr}{files.ListToString(vbCr)}", MsgTitle})
575+
If Not isCopy And updateFileLocations Then
576+
If downloaderFilesUpdated Then Downloader.FilesSave()
577+
If FeedMode = FeedModes.Saved And Not LoadedSessionName.IsEmptyString And filesReplace.Count > 0 Then
578+
sesFile = $"{TDownloader.SessionsPath.CSFilePS}{LoadedSessionName}.xml"
579+
If sesFile.Exists Then
580+
sessionData.Clear()
581+
x = New XmlFile(sesFile, Protector.Modes.All, False) With {.AllowSameNames = True}
582+
x.LoadData()
583+
If x.Count > 0 Then sessionData.ListAddList(x)
584+
x.Dispose()
585+
If sessionData.Count > 0 Then
586+
For Each rfile As KeyValuePair(Of SFile, SFile) In filesReplace
587+
ff = rfile.Key
588+
df = rfile.Value
589+
indx = sessionData.FindIndex(finder)
590+
If indx >= 0 Then
591+
mm = sessionData(indx)
592+
mm_data = mm.Data
593+
mm_data.File = df
594+
mm = New UserMediaD(mm_data, mm.User, mm.Session, mm.Date)
595+
sessionData(indx) = mm
596+
sesFilesReplaced = True
597+
End If
598+
Next
599+
If sesFilesReplaced Then
600+
x = New XmlFile With {.AllowSameNames = True}
601+
x.AddRange(sessionData)
602+
x.Name = TDownloader.Name_SessionXML
603+
x.Save(sesFile, EDP.SendToLog)
604+
x.Dispose()
605+
End If
606+
sessionData.Clear()
607+
End If
608+
End If
609+
End If
610+
If filesReplace.Count > 0 Then filesReplace.ForEach(Sub(fr) Settings.Feeds.UpdateDataByFile(fr.Key, fr.Value))
611+
filesReplace.Clear()
612+
RefillList()
613+
End If
614+
If IsInternal Then MsgBoxE({$"The following files were {IIf(isCopy, "copied", "moved")} to{vbCr}{dest}{vbCr}{vbCr}{files.ListToString(vbCr)}", MsgTitle})
521615
End If
522616
Else
523617
MsgBoxE({"No files selected", MsgTitle}, vbExclamation)
524618
End If
619+
Return result
525620
Catch ex As Exception
526-
ErrorsDescriber.Execute(EDP.LogMessageValue, ex, MsgTitle)
621+
Return ErrorsDescriber.Execute(EDP.LogMessageValue, ex, MsgTitle, False)
622+
Finally
623+
Settings.Feeds.UpdateWhereDataReplaced()
527624
End Try
528-
End Sub
625+
End Function
529626
#Region "Load fav, spec"
530627
Private Sub BTT_LOAD_FAV_Click(sender As Object, e As EventArgs) Handles BTT_LOAD_FAV.Click
531628
FeedChangeMode(FeedModes.Special, {FeedSpecial.FavoriteName})
@@ -713,7 +810,7 @@ Namespace DownloadObjects
713810
If MsgBoxE({"Are you sure you want to clear this session data?", "Clear session"}, vbExclamation,,, {"Process", "Cancel"}) = 0 Then
714811
Downloader.Files.Clear()
715812
ClearTable()
716-
RefillList0()
813+
RefillList()
717814
End If
718815
End Sub
719816
#End Region
@@ -848,18 +945,21 @@ Namespace DownloadObjects
848945
Settings.FeedLastModeSubscriptions.Value = OPT_SUBSCRIPTIONS.Checked
849946
MENU_DOWN.Visible = OPT_SUBSCRIPTIONS.Checked
850947
End Sub, EDP.None)
851-
If __refill Then RefillList0()
948+
If __refill Then RefillList()
852949
End Sub
853950
#End Region
854951
Friend Sub Downloader_FilesChanged(ByVal Added As Boolean)
855952
ControlInvokeFast(ToolbarTOP, BTT_REFRESH, Sub() BTT_REFRESH.ToolTipText = If(Added, "New files found", "Some files have been removed"))
856953
BTT_REFRESH.ControlChangeColor(ToolbarTOP, Added, False)
857954
End Sub
858955
Private Sub BTT_REFRESH_Click(sender As Object, e As EventArgs) Handles BTT_REFRESH.Click
859-
RefillList0()
956+
RefillList()
860957
End Sub
861958
#End Region
862959
#Region "FeedMedia handlers"
960+
Private Sub FeedMedia_MediaMove(ByVal Sender As FeedMedia, ByVal Destination As SFile, ByRef Result As Boolean)
961+
Result = MoveCopyFiles(False, Nothing, Destination, Sender.File)
962+
End Sub
863963
Private Sub FeedMedia_MediaDeleted(ByVal Sender As FeedMedia)
864964
Try
865965
ControlInvoke(TP_DATA, Sub() TPRemoveControl(Sender, True))
@@ -1061,7 +1161,7 @@ Namespace DownloadObjects
10611161
If d.ListExists AndAlso Not IsSubscription AndAlso d.All(FileNotExist) Then
10621162
i = Sender.CurrentIndex
10631163
Sender.HandlersSuspended = True
1064-
RefillList0(False)
1164+
RefillList(False)
10651165
If Sender.Count > 0 Then
10661166
If i.ValueBetween(0, Sender.Count - 1) Then Sender.CurrentIndex = i
10671167
Sender.HandlersSuspended = False
@@ -1087,6 +1187,7 @@ Namespace DownloadObjects
10871187
With fmList.Last
10881188
AddHandler .MediaDeleted, AddressOf FeedMedia_MediaDeleted
10891189
AddHandler .MediaDownload, AddressOf FeedMedia_Download
1190+
AddHandler .MediaMove, AddressOf FeedMedia_MediaMove
10901191
AddHandler .FeedAddWithRemove, AddressOf FeedMedia_FeedAddWithRemove
10911192
End With
10921193
End Sub)

SCrawler/Download/Feed/FeedMedia.vb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Namespace DownloadObjects
1818
Friend Event MediaDeleted(ByVal Sender As Object)
1919
Friend Event MediaDownload As EventHandler
2020
Friend Event FeedAddWithRemove(ByVal Sender As FeedMedia, ByVal Feeds As IEnumerable(Of String), ByVal Media As UserMediaD, ByVal RemoveOperation As Boolean)
21+
Friend Event MediaMove(ByVal Sender As FeedMedia, ByVal Destination As SFile, ByRef Result As Boolean)
2122
#End Region
2223
#Region "Declarations"
2324
Private Const VideoHeight As Integer = 450
@@ -490,18 +491,20 @@ Namespace DownloadObjects
490491
Dim isCopy As Boolean = sender Is BTT_COPY_TO
491492
Dim dest As SFile = Nothing
492493
Dim ff As SFile = File
494+
Dim result As Boolean = False
495+
493496
Using f As New FeedCopyToForm({File}, isCopy)
494497
f.ShowDialog()
495498
If f.DialogResult = DialogResult.OK Then dest = f.Destination
496499
End Using
497500
If Not dest.IsEmptyString Then
498501
ff.Path = dest
499502
If isCopy Then
500-
File.Copy(ff)
503+
result = File.Copy(ff)
501504
Else
502-
If SFile.Move(File, ff) Then RaiseEvent MediaDeleted(Me)
505+
RaiseEvent MediaMove(Me, dest, result)
503506
End If
504-
MsgBoxE({$"File {IIf(isCopy, "copied", "moved")}{vbCr}Source: '{File}'{vbCr}Destination: '{ff}'", MsgTitle})
507+
If result Then MsgBoxE({$"File {IIf(isCopy, "copied", "moved")}{vbCr}Source: '{File}'{vbCr}Destination: '{ff}'", MsgTitle})
505508
End If
506509
End If
507510
Catch ex As Exception

SCrawler/Download/Feed/FeedSpecial.vb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Namespace DownloadObjects
3030
Friend Const FavoriteName As String = "Favorite"
3131
Friend Const SpecialName As String = "Special"
3232
Private ReadOnly Items As List(Of UserMediaD)
33+
Private _FilesUpdated As Boolean = False
3334
Private _File As SFile
3435
Friend ReadOnly Property File As SFile
3536
Get
@@ -167,6 +168,26 @@ Namespace DownloadObjects
167168
Return Item
168169
End Function
169170
#End Region
171+
#Region "UpdateDataByFile"
172+
Friend Sub UpdateDataByFile(ByVal InitialFile As SFile, ByVal NewFile As SFile)
173+
Try
174+
Dim indx% = Items.FindIndex(Function(ii) ii.Data.File = InitialFile)
175+
If indx >= 0 Then
176+
Dim m As UserMediaD = Items(indx)
177+
Dim mm As UserMedia = m.Data
178+
mm.File = NewFile
179+
m = New UserMediaD(mm, m.User, m.Session, m.Date)
180+
Items(indx) = m
181+
_FilesUpdated = True
182+
End If
183+
Catch ex As Exception
184+
ErrorsDescriber.Execute(EDP.SendToLog, ex, "[FeedSpecial.UpdateDataByFile]")
185+
End Try
186+
End Sub
187+
Friend Sub UpdateIfRequired()
188+
If _FilesUpdated Then Save() : _FilesUpdated = False
189+
End Sub
190+
#End Region
170191
#Region "Add"
171192
Friend Overloads Function Add(ByVal Item As UserMediaD, Optional ByVal AutoSave As Boolean = True) As Boolean
172193
If Not Items.Contains(Item) Then

SCrawler/Download/Feed/FeedSpecialCollection.vb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Namespace DownloadObjects
174174
End Get
175175
End Property
176176
#End Region
177-
#Region "Add, Delete"
177+
#Region "Add, Delete, UpdateDataByFile, UpdateWhereDataReplaced"
178178
Friend Function Add(ByVal Name As String) As Integer
179179
Dim i% = -1
180180
If Not Name.IsEmptyString Then
@@ -219,6 +219,12 @@ Namespace DownloadObjects
219219
End If
220220
Return result
221221
End Function
222+
Friend Sub UpdateDataByFile(ByVal InitialFile As SFile, ByVal NewFile As SFile)
223+
If Count > 0 Then Feeds.ForEach(Sub(f) f.UpdateDataByFile(InitialFile, NewFile))
224+
End Sub
225+
Friend Sub UpdateWhereDataReplaced()
226+
If Count > 0 Then Feeds.ForEach(Sub(f) f.UpdateIfRequired())
227+
End Sub
222228
#End Region
223229
#Region "IndexOf"
224230
Friend Function IndexOf(ByVal Name As String) As Integer

0 commit comments

Comments
 (0)