Check-in [164505d2e1]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:ZPreview: streamlined download of release.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | feature/auto-update
Files: files | file ages | folders
SHA1: 164505d2e14ef8cc28e4c06cb415e781350cf96c
User & Date: Martijn 2015-02-25 18:02:08
Context
2015-02-25
18:25
Releases are now listed in the correct order. check-in: 99141eba3c user: Martijn tags: feature/auto-update
18:02
ZPreview: streamlined download of release. check-in: 164505d2e1 user: Martijn tags: feature/auto-update
14:40
ZPreview auto-update appears to work: downloads the zip file, replaces the existing files by those in the zip, and restarts. check-in: 8b24d78ff0 user: tinus tags: feature/auto-update
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ZPreview/src/Delphi/F_AutoUpdate.pas.

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218


219
220
221
222
223
224

225
226
227
228
229
230
231
232
233
234
235
...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

266
267
268
269
270
271
272
273

    FXmlFeed := TNativeXml.Create(nil);
    try
//      URL := 'http://fossil.2of4.net/zaap/timeline.rss?y=ci&tag=zpreview&name=ZPreview.'+{$IFDEF WIN32}'32'{$ELSE}'64'{$ENDIF}+'.exe';
      URL := FBaseURL + 'timeline.rss?y=ci';
      if FTagName <> '' then
        URL := URL + '&tag=' + FTagName; // TODO: URLEncode
      URL := URL + '&name=' + ExtractFileName(Application.ExeName); // TODO: URLEncode;

      FXmlFeed.LoadFromURL(UTF8String(URL));
    except
      FreeAndNil(FXmlFeed);
      raise;
    end;
  end;
................................................................................
    FreeAndNil(FForm);
  end;
end {TUpdateChecker.AskUserForUpdate};

{ ------------------------------------------------------------------------------------------------ }
procedure TUpdateChecker.UpdateToVersion(const UUID: string);
var
  BackupPrefix, WorkPath: string;
  ZipStream: TStream;
  Zip: TZipFile;
  i: Integer;
  FileName, PathName: string;
  RunSelf: string;
  SI: TStartupInfo;
  PI: TProcessInformation;
begin
  WorkPath := ExtractFilePath(Application.ExeName);
  BackupPrefix := '~'; // TODO: make this version-based

  // download the zip from http://fossil.2of4.net/zaap/zip/zpreview.zip?uuid={UUID}
  // TODO: pass a routine to update the progress bar
  ZipStream := DownloadFromURL(FBaseURL + 'zip/release.zip?uuid=' + UUID); // TODO: URLEncode


  try
    Zip := TZipFile.Create;
    try
      Zip.Open(ZipStream, zmRead);

      // extract each file, and try to rename an eventual existing file.

      for i := 0 to Zip.FileCount - 1 do begin
        FileName := Zip.FileName[i];
        Assert(FileName.StartsWith('release/'));
        FileName := FileName.Substring(8).Replace('/', PathDelim);
        PathName := ExtractFilePath(FileName);
        FileName := ExtractFileName(FileName);
        if FileName <> '' then begin
          if PathName <> '' then
            ForceDirectories(WorkPath + PathName);

          // Test for existing file, and rename that
................................................................................
          {$MESSAGE HINT 'TODO: Save all new files to a temp file in the same dir, then afterwards, use ReplaceFile for each of them  MCO 25-02-2015'}
          if FileExists(WorkPath + PathName + FileName) then begin
            RenameFile(WorkPath + PathName + FileName, WorkPath + PathName + BackupPrefix + FileName);
          end;

          Zip.Extract(i, WorkPath + PathName, False);

          // adjust the new file's times
          TFile.SetLastWriteTime(WorkPath + PathName + FileName, FileDateToDateTime(Zip.FileInfo[i].ModifiedDateTime));
        end;
      end;

      // TODO: use transacted file operations, so we can roll them back if something fails?
    finally
      Zip.Free;
................................................................................
    end;
  finally
    ZipStream.Free;
  end;

  {$MESSAGE WARN 'TODO: TUpdateChecker.UpdateToVersion  MCO 25-02-2015'}

  RunSelf := Application.ExeName;

  // TODO: close the current application, and start a new one
  Application.Tag := -1;
  Application.OnException := nil;
  Application.Terminate;
  Application.MainForm.Close;


  if not CreateProcess(nil, PChar(RunSelf), nil, nil, True, 0, nil, nil, SI, PI) then
    RaiseLastOSError;
end {TUpdateChecker.UpdateToVersion};

{ ------------------------------------------------------------------------------------------------ }
function TUpdateChecker.DownloadFromURL(const URL: string): TStream;
var
  UserAgent: string;







|







 







|




|




|


|
|
>
>






>


|
|







 







|







 







|

|
|




>
|







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

    FXmlFeed := TNativeXml.Create(nil);
    try
//      URL := 'http://fossil.2of4.net/zaap/timeline.rss?y=ci&tag=zpreview&name=ZPreview.'+{$IFDEF WIN32}'32'{$ELSE}'64'{$ENDIF}+'.exe';
      URL := FBaseURL + 'timeline.rss?y=ci';
      if FTagName <> '' then
        URL := URL + '&tag=' + FTagName; // TODO: URLEncode
//      URL := URL + '&name=' + ExtractFileName(Application.ExeName); // TODO: URLEncode;

      FXmlFeed.LoadFromURL(UTF8String(URL));
    except
      FreeAndNil(FXmlFeed);
      raise;
    end;
  end;
................................................................................
    FreeAndNil(FForm);
  end;
end {TUpdateChecker.AskUserForUpdate};

{ ------------------------------------------------------------------------------------------------ }
procedure TUpdateChecker.UpdateToVersion(const UUID: string);
var
  WorkPath, PathPrefix, BackupPrefix: string;
  ZipStream: TStream;
  Zip: TZipFile;
  i: Integer;
  FileName, PathName: string;
  ExeName: string;
  SI: TStartupInfo;
  PI: TProcessInformation;
begin
  WorkPath := ExtractFilePath(Application.ExeName);
  BackupPrefix := '~'; // TODO: make this version-based (version of the currently running executable)

  // download the zip from http://fossil.2of4.net/zaap/zip/zpreview.zip?uuid={UUID}
  // TODO: pass a routine to update the progress bar?
  ZipStream := DownloadFromURL(FBaseURL + 'zip/' + FTagName + '.zip?uuid=' + UUID); // TODO: URLEncode
  if not Assigned(ZipStream) then
    raise Exception.Create('Failed to download release ' + UUID + '.');
  try
    Zip := TZipFile.Create;
    try
      Zip.Open(ZipStream, zmRead);

      // extract each file, and try to rename an eventual existing file.
      PathPrefix := FTagName + '/';
      for i := 0 to Zip.FileCount - 1 do begin
        FileName := Zip.FileName[i];
        Assert(FileName.StartsWith(PathPrefix));
        FileName := FileName.Substring(PathPrefix.Length).Replace('/', PathDelim);
        PathName := ExtractFilePath(FileName);
        FileName := ExtractFileName(FileName);
        if FileName <> '' then begin
          if PathName <> '' then
            ForceDirectories(WorkPath + PathName);

          // Test for existing file, and rename that
................................................................................
          {$MESSAGE HINT 'TODO: Save all new files to a temp file in the same dir, then afterwards, use ReplaceFile for each of them  MCO 25-02-2015'}
          if FileExists(WorkPath + PathName + FileName) then begin
            RenameFile(WorkPath + PathName + FileName, WorkPath + PathName + BackupPrefix + FileName);
          end;

          Zip.Extract(i, WorkPath + PathName, False);

          // adjust the new file's modified time
          TFile.SetLastWriteTime(WorkPath + PathName + FileName, FileDateToDateTime(Zip.FileInfo[i].ModifiedDateTime));
        end;
      end;

      // TODO: use transacted file operations, so we can roll them back if something fails?
    finally
      Zip.Free;
................................................................................
    end;
  finally
    ZipStream.Free;
  end;

  {$MESSAGE WARN 'TODO: TUpdateChecker.UpdateToVersion  MCO 25-02-2015'}

  ExeName := Application.ExeName;

  // Close the current instance...
  Application.Tag := -1; // signal for the single-instance algorithm that we're shutting down
  Application.OnException := nil;
  Application.Terminate;
  Application.MainForm.Close;

  // ...and start a new one
  if not CreateProcess(nil, PChar(ExeName), nil, nil, True, 0, nil, nil, SI, PI) then
    RaiseLastOSError;
end {TUpdateChecker.UpdateToVersion};

{ ------------------------------------------------------------------------------------------------ }
function TUpdateChecker.DownloadFromURL(const URL: string): TStream;
var
  UserAgent: string;

Changes to ZPreview/src/Delphi/F_Main.dfm.

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    ParentShowHint = False
    ShowHint = True
    Spacing = 0
    ExplicitHeight = 26
  end
  object pnlRight: TPanel
    Left = 812
    Top = 64
    Width = 4
    Height = 421
    Align = alRight







<







72
73
74
75
76
77
78

79
80
81
82
83
84
85
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    ParentShowHint = False
    ShowHint = True
    Spacing = 0

  end
  object pnlRight: TPanel
    Left = 812
    Top = 64
    Width = 4
    Height = 421
    Align = alRight